File Exchange Pick of the Week

Our best user submissions

Be Absolute about your Relative Path with GetFullPath 4

Posted by Jiro Doke,

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 =
imgFile1 =

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 =

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 =

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.


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

Get the MATLAB code

Published with MATLAB® 7.11


Comments are closed.

4 CommentsOldest to Newest

Jotaf replied on : 1 of 4
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.
Jan Simon replied on : 2 of 4
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
Jotaf replied on : 3 of 4
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!
Julian replied on : 4 of 4
I too often need an absolute path and have employed various solutions over the years. I am not surprised Jiro mentions a little embarrassment, because such a commonly needed operation should be a breeze in a mature environment... You may also want to use absolute path relative to the current location before testing for existence of a file or a folder, because the built in existence tests search everywhere on the MATLAB path which may not be your intention. Here are some things I tried (restricted to Windows): cd(cd('..\..')) % folders only, and only if they exist, and changes folder temporarily which is not welcome which(test) % resolves absolute path only for files, but anywhere on matlab path not just with respect to current dir what(test) % resolves absolute path only for folders, but expands partial paths anywhere on matlab path fileattrib(test) % works for files and folders, and (dangerously) allows wild cards, but only if they exist already A object has a method getCanonicalPath, so it should be easy to use java from MATLAB to solve the problem. Unfortunately on my PC'\').getCanonicalPath returns the root drive of my userpath and not the current path so this natural solution fails for me. That is a shame because the java solution should be platform independent. I am actually using a Microsoft solution using actxserver('Scripting.FileSystemObject') which has a method GetAbsolutePathName, and I can split up parts using GetBaseName, GetExtensionName, & GetDriveName methods. This is easier for me to follow/extend than the C / Win-API approach, but no doubt is slower. A third Microsoft route which I haven't tried would be to use similar methods offered by the .NET framework. Please Mr. MathWorks can you consider building something into MATLAB to make this very very common coding pattern both simple and fast? I would have thought you can build a bridge to built in OS libraries rather than use slow cumbersome string manipulation in MATLAB.