{"id":2723,"date":"2011-04-01T14:15:23","date_gmt":"2011-04-01T14:15:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/04\/01\/be-absolute-about-your-relative-path-with-getfullpath\/"},"modified":"2016-11-11T05:35:16","modified_gmt":"2016-11-11T10:35:16","slug":"be-absolute-about-your-relative-path-with-getfullpath","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/04\/01\/be-absolute-about-your-relative-path-with-getfullpath\/","title":{"rendered":"Be Absolute about your Relative Path with GetFullPath"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28249-getfullpath\"><tt>GetFullPath<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15233\">Jan<\/a>.\r\n   <\/p>\r\n   <p>Jan is no stranger to good File Exchange entries. I've already highlighted two of his entries before (<a href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/10\/22\/protecting-your-confidential-files\/\">cryptAES<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/08\/21\/close-that-dialog\/\">AutoWarnDlg<\/a>). This one is a very handy utility for those working with files in different directories and needing an easy way to get absolute\r\n      path names from relative or partial names. I'm a little embarrassed to say that I didn't see the pain I was having until I\r\n      came across Jan's entry. In various MATLAB programs, I have used code that looked like this. I'm using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/fileparts.html\"><tt>fileparts<\/tt><\/a> to go up directories and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/fullfile.html\"><tt>fullfile<\/tt><\/a> to construct paths.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">curDir   = pwd\r\nmainDir  = fileparts(fileparts(curDir));            <span style=\"color: #228B22\">% go up 2 directories<\/span>\r\nimgFile1 = fullfile(mainDir, <span style=\"color: #A020F0\">'Internal'<\/span>, <span style=\"color: #A020F0\">'GoogleMap.png'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Check that the file exists<\/span>\r\nassert(exist(imgFile1, <span style=\"color: #A020F0\">'file'<\/span>) == 2, <span style=\"color: #A020F0\">'File does not exist'<\/span>)<\/pre><pre style=\"font-style:oblique\">curDir =\r\nC:\\MyStuff\\Work\\NoCopy\\Blog\\POTW\\FullPath\r\nimgFile1 =\r\nC:\\MyStuff\\Work\\NoCopy\\Blog\\Internal\\GoogleMap.png\r\n<\/pre><p><p>I can use relative path along with <tt>fullfile<\/tt>, but the result looks kind of messy:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imgFile2 = fullfile(curDir, <span style=\"color: #A020F0\">'..'<\/span>, <span style=\"color: #A020F0\">'..'<\/span>, <span style=\"color: #A020F0\">'Internal'<\/span>, <span style=\"color: #A020F0\">'GoogleMap.png'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Check that the file exists<\/span>\r\nassert(exist(imgFile2, <span style=\"color: #A020F0\">'file'<\/span>) == 2, <span style=\"color: #A020F0\">'File does not exist'<\/span>);<\/pre><pre style=\"font-style:oblique\">imgFile2 =\r\nC:\\MyStuff\\Work\\NoCopy\\Blog\\POTW\\FullPath\\..\\..\\Internal\\GoogleMap.png\r\n<\/pre><p><p>With Jan's <tt>GetFullPath<\/tt>, I get exactly what I want, with a simple, familiar syntax:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imgFile3 = GetFullPath(<span style=\"color: #A020F0\">'..\\..\\Internal\\GoogleMap.png'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Check that the file exists<\/span>\r\nassert(exist(imgFile3, <span style=\"color: #A020F0\">'file'<\/span>) == 2, <span style=\"color: #A020F0\">'File does not exist'<\/span>);<\/pre><pre style=\"font-style:oblique\">imgFile3 =\r\nC:\\MyStuff\\Work\\NoCopy\\Blog\\Internal\\GoogleMap.png\r\n<\/pre><p><p>If you're familiar with some of Jan's entries, he has included a fast C-Mex version of the code (Windows-only), but for people\r\n      like me who breathe MATLAB, the .m version is enough to evoke a smile. He also includes a unit test suite for his function,\r\n      which is always a good practice.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2723#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28249-getfullpath#comments\">comment<\/a> for Jan.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_af4a57dcd7b84ac09b2c635950b2df10() {\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='af4a57dcd7b84ac09b2c635950b2df10 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' af4a57dcd7b84ac09b2c635950b2df10';\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 = 'Jiro Doke';\r\n        copyright = 'Copyright 2011 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_af4a57dcd7b84ac09b2c635950b2df10()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\naf4a57dcd7b84ac09b2c635950b2df10 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28249-getfullpath\r\n% |GetFullPath|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15233 Jan>.\r\n%\r\n% Jan is no stranger to good File Exchange entries. I've already\r\n% highlighted two of his entries before\r\n% (<https:\/\/blogs.mathworks.com\/pick\/2010\/10\/22\/protecting-your-confidential-files\/\r\n% cryptAES> and\r\n% <https:\/\/blogs.mathworks.com\/pick\/2009\/08\/21\/close-that-dialog\/\r\n% AutoWarnDlg>). This one is a very handy utility for those working with\r\n% files in different directories and needing an easy way to get absolute\r\n% path names from relative or partial names. I'm a little embarrassed to\r\n% say that I didn't see the pain I was having until I came across Jan's\r\n% entry. In various MATLAB programs, I have used code that looked like\r\n% this. I'm using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/fileparts.html\r\n% |fileparts|> to go up directories and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/fullfile.html\r\n% |fullfile|> to construct paths.\r\n\r\ncurDir   = pwd\r\nmainDir  = fileparts(fileparts(curDir));            % go up 2 directories\r\nimgFile1 = fullfile(mainDir, 'Internal', 'GoogleMap.png')\r\n\r\n% Check that the file exists\r\nassert(exist(imgFile1, 'file') == 2, 'File does not exist')\r\n\r\n%%\r\n% I can use relative path along with |fullfile|, but the result looks kind\r\n% of messy:\r\n\r\nimgFile2 = fullfile(curDir, '..', '..', 'Internal', 'GoogleMap.png')\r\n\r\n% Check that the file exists\r\nassert(exist(imgFile2, 'file') == 2, 'File does not exist');\r\n\r\n%%\r\n% With Jan's |GetFullPath|, I get exactly what I want, with a simple,\r\n% familiar syntax:\r\n\r\nimgFile3 = GetFullPath('..\\..\\Internal\\GoogleMap.png')\r\n\r\n% Check that the file exists\r\nassert(exist(imgFile3, 'file') == 2, 'File does not exist');\r\n\r\n%%\r\n% If you're familiar with some of Jan's entries, he has included a fast\r\n% C-Mex version of the code (Windows-only), but for people like me who\r\n% breathe MATLAB, the .m version is enough to evoke a smile. He also\r\n% includes a unit test suite for his function, which is always a good\r\n% practice. Anyone interested in incorporating unit testing in your code,\r\n% take a look at\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22846-matlab-xunit-test-framework\r\n% this>.\r\n%\r\n% *Comments*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2723#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28249-getfullpath#comments\r\n% comment> for Jan.\r\n\r\n##### SOURCE END ##### af4a57dcd7b84ac09b2c635950b2df10\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is GetFullPath by Jan.\r\n   \r\n   Jan is no stranger to good File Exchange entries. I've already highlighted two of his entries before (cryptAES and AutoWarnDlg). This one is... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/04\/01\/be-absolute-about-your-relative-path-with-getfullpath\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2723"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2723"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2723\/revisions"}],"predecessor-version":[{"id":8035,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2723\/revisions\/8035"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}