{"id":87,"date":"2007-04-13T08:42:06","date_gmt":"2007-04-13T13:42:06","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/13\/machine-independent-filenames\/"},"modified":"2007-04-09T08:43:37","modified_gmt":"2007-04-09T13:43:37","slug":"machine-independent-filenames","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/13\/machine-independent-filenames\/","title":{"rendered":"Machine-Independent Filenames"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>When developing applications is MATLAB, I often find myself interacting with files, sometimes data, sometimes algorithms.\r\n         In order to be sure I am operating with the correct file, for example for loading data, I want to use the full path.  However,\r\n         I also want to pass my program on to other users, and their path information might not match mine.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Recent Example at The MathWorks<\/a><\/li>\r\n         <li><a href=\"#5\">Reference Material<\/a><\/li>\r\n         <li><a href=\"#6\">Comments<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Recent Example at The MathWorks<a name=\"1\"><\/a><\/h3>\r\n   <p>Recently at The MathWorks, someone wanted to share some files with another person so they could discuss the content.  To do\r\n      so, the author provided code that looked like this.  Here <tt>YEAR<\/tt> is a variable of class <tt>char<\/tt>.\r\n   <\/p><pre>   load(['T:\\1-36U6NN\\secprd',YEAR,'.txt'])<\/pre><p>You might wonder what the problem with this is.  Well, for one thing, the author works on a Windows platform, but the recipient\r\n      frequently does not.  So how could this code have been written differently?\r\n   <\/p>\r\n   <p>The <tt>T:\\<\/tt> drive maps to a file server that is accessible from any of our networked computers.  So one possibility is to write code\r\n      something like this:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">      baseLocationWin = <span style=\"color: #A020F0\">'T:\\'<\/span>;\r\n      baseLocationOther = <span style=\"color: #A020F0\">'\/\/home\/tester'<\/span>;\r\n      <span style=\"color: #0000FF\">if<\/span> ispc  <span style=\"color: #228B22\">% true for windows platforms<\/span>\r\n          baseLocation = baseLocationWin;\r\n      <span style=\"color: #0000FF\">else<\/span>\r\n          baseLocation = baseLocationOther;\r\n      <span style=\"color: #0000FF\">end<\/span>\r\n      path1 = <span style=\"color: #A020F0\">'1-36U6NN'<\/span>;\r\n      YEAR = <span style=\"color: #A020F0\">'2001'<\/span>;\r\n      fname = [<span style=\"color: #A020F0\">'secprd'<\/span> YEAR <span style=\"color: #A020F0\">'.txt'<\/span>];\r\n      fullfname = fullfile(baseLocation, path1, fname);<\/pre><p>On Windows, where I ran this code, here's the file name.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(fullfname)<\/pre><pre style=\"font-style:oblique\">T:\\1-36U6NN\\secprd2001.txt\r\n<\/pre><p>The equivalent UNIX pathname would be<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp([baseLocationOther <span style=\"color: #A020F0\">'\/'<\/span> path1, <span style=\"color: #A020F0\">'\/'<\/span> fname])<\/pre><pre style=\"font-style:oblique\">\/\/home\/tester\/1-36U6NN\/secprd2001.txt\r\n<\/pre><h3>Reference Material<a name=\"5\"><\/a><\/h3>\r\n   <p>Here you'll see a list of some MATLAB functions that are useful in writing machine independent applications when it comes\r\n      to referring to files.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/which.html\"><tt>which<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fullfile.html\"><tt>fullfile<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/filesep.html\"><tt>filesep<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/pathsep.html\"><tt>pathsep<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/computer.html\"><tt>computer<\/tt><\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Comments<a name=\"6\"><\/a><\/h3>\r\n   <p>Have you been in this situation? Do you have other techniques to help users write robust code for handling files?  If so,\r\n      please post <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=87#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_dc875eca1d7741ef8aa6f346a9b6f3fd() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='dc875eca1d7741ef8aa6f346a9b6f3fd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' dc875eca1d7741ef8aa6f346a9b6f3fd';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_dc875eca1d7741ef8aa6f346a9b6f3fd()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.4<br><\/p>\r\n<\/div>\r\n<!--\r\ndc875eca1d7741ef8aa6f346a9b6f3fd ##### SOURCE BEGIN #####\r\n%% Machine-Independent Filenames\r\n% When developing applications is MATLAB, I often find myself\r\n% interacting with files, sometimes data, sometimes algorithms.  In order\r\n% to be sure I am operating with the correct file, for example for\r\n% loading data, I want to use the full path.  However, I also want to pass\r\n% my program on to other users, and their path information might not match\r\n% mine.\r\n%% Recent Example at The MathWorks\r\n% Recently at The MathWorks, someone wanted to share some files with\r\n% another person so they could discuss the content.  To do so, the author\r\n% provided code that looked like this.  Here |YEAR| is a variable of class\r\n% |char|.\r\n%\r\n%     load(['T:\\1-36U6NN\\secprd',YEAR,'.txt'])\r\n%\r\n% You might wonder what the problem with this is.  Well, for one thing, the\r\n% author works on a Windows platform, but the recipient frequently does\r\n% not.  So how could this code have been written differently?\r\n%%\r\n% The |T:\\| drive maps to a file server that is accessible from any of our\r\n% networked computers.  So one possibility is to write code something like\r\n% this:\r\n      baseLocationWin = 'T:\\';\r\n      baseLocationOther = '\/\/home\/tester';\r\n      if ispc  % true for windows platforms\r\n          baseLocation = baseLocationWin;\r\n      else\r\n          baseLocation = baseLocationOther;\r\n      end\r\n      path1 = '1-36U6NN';\r\n      YEAR = '2001';\r\n      fname = ['secprd' YEAR '.txt'];\r\n      fullfname = fullfile(baseLocation, path1, fname);\r\n%%\r\n% On Windows, where I ran this code, here's the file name.\r\ndisp(fullfname)\r\n%%\r\n% The equivalent UNIX pathname would be\r\ndisp([baseLocationOther '\/' path1, '\/' fname])\r\n\r\n%% Reference Material\r\n% Here you'll see a list of some MATLAB functions that are useful in\r\n% writing machine independent applications when it comes to referring to\r\n% files.\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/which.html |which|>\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fullfile.html |fullfile|>\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/filesep.html |filesep|>\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/pathsep.html |pathsep|>\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/computer.html |computer|>\r\n%% Comments\r\n% Have you been in this situation? Do you have other techniques to help\r\n% users write robust code for handling files?  If so, please post\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=87#respond here>.\r\n\r\n\r\n\r\n\r\n##### SOURCE END ##### dc875eca1d7741ef8aa6f346a9b6f3fd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      When developing applications is MATLAB, I often find myself interacting with files, sometimes data, sometimes algorithms.\r\n         In order to be sure I am operating with the correct... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/13\/machine-independent-filenames\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}