File Exchange Pick of the Week

April 1st, 2011

Be Absolute about your Relative Path with GetFullPath

Jiro's pick this week is GetFullPath by Jan.

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 very handy utility for those working with files in different directories and needing an easy way to get absolute 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 came across Jan's entry. In various MATLAB programs, I have used code that looked like this. I'm using fileparts to go up directories and fullfile to construct paths.

curDir   = pwd
mainDir  = fileparts(fileparts(curDir));            % go up 2 directories
imgFile1 = fullfile(mainDir, 'Internal', 'GoogleMap.png')

% Check that the file exists
assert(exist(imgFile1, 'file') == 2, 'File does not exist')
curDir =
C:\MyStuff\Work\NoCopy\Blog\POTW\FullPath
imgFile1 =
C:\MyStuff\Work\NoCopy\Blog\Internal\GoogleMap.png

I can use relative path along with fullfile, but the result looks kind of messy:

imgFile2 = fullfile(curDir, '..', '..', 'Internal', 'GoogleMap.png')

% Check that the file exists
assert(exist(imgFile2, 'file') == 2, 'File does not exist');
imgFile2 =
C:\MyStuff\Work\NoCopy\Blog\POTW\FullPath\..\..\Internal\GoogleMap.png

With Jan's GetFullPath, I get exactly what I want, with a simple, familiar syntax:

imgFile3 = GetFullPath('..\..\Internal\GoogleMap.png')

% Check that the file exists
assert(exist(imgFile3, 'file') == 2, 'File does not exist');
imgFile3 =
C:\MyStuff\Work\NoCopy\Blog\Internal\GoogleMap.png

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 like me who breathe MATLAB, the .m version is enough to evoke a smile. He also includes a unit test suite for his function, which is always a good practice. Anyone interested in incorporating unit testing in your code, take a look at this.

Comments

Let us know what you think here or leave a comment for Jan.


Get the MATLAB code

Published with MATLAB® 7.11

3 Responses to “Be Absolute about your Relative Path with GetFullPath”

  1. Jotaf replied on :

    I can’t see the difference between:

    imgFile3 = GetFullPath(‘..\..\Internal\GoogleMap.png’)

    And:

    imgFile3 = [curDir '/../../Internal/GoogleMap.png'];

    Can someone tell me what subtleties I am missing here? I’ve used this extensively to access different folders. Forward slashes ‘/’ work both on Windows and Linux so I prefer them over ‘\’. On Windows I’ve mixed the two styles, and multiple ‘..’ commands, without any problems. I’m sure I’m missing something important here.

  2. Jan Simon replied on :

    Dear Jotaf,

    Both versions are working to access a file:
    file = GetFullPath(‘..\..\Internal\pic.png’)
    And:
    file = [cd, '/../../Internal/pic.png'];
    The later works even on ‘C:\’, although ‘C:\/../../Internal/’ is ugly.
    Now imagine you need to process the file name itself, e.g. to check if it is member of a list of a already processed files:
    file1 = [cd, '/../../Internal/pic.png'];
    file2 = [cd, '/../A/../../Internal/pic.png'];
    Both names are different, but they point to the same file! GetFullPath will reveal this equality, because it creates the “canonical” path: shortest, unique, absoulte.

    Getting the name of the parent folder is hard, if ‘/..’ have to be taken into account, while it is easy using FILEPARTS for a canonical path.

    Another important application is getting a file name as user-defined input: The name can be absoulte or relative, and [cd, '/', FileName] will not help anymore. Using a relative path can lead to errors, if the current directory is changed unexpectedly in the program (multi-threading!). Therefore a qualified path is safer and easy to obtain by GetFullPath, because it accepts absolute *and* relative paths.

    Kind regards, Jan

  3. Jotaf replied on :

    Right, as I thought it’s not for the straight application of specifying relative paths but rather for a few other subtle, but important, variations. Thanks!

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.