File Exchange

image thumbnail

Relative Filename

version 1.9 (4.97 KB) by

A pair of M-files to convert between full and relative filenames.

1 Download

Updated

View License

RELFILE(FILE,PATH) converts full filename FILE to a relative filename with respect to PATH.

REL2FULLFILE(RELFILE,PATH) converts relative filename RELFILE to full filename using PATH as the base path.

Examples:

relfile('C:\Program Files\MATLAB\R2009a\bin\matlab.exe','C:\Program Files\MATLAB\R2009a')

returns '.\bin\matlab.exe' and

rel2fullfile('.\bin\matlab.exe','C:\Program Files\MATLAB\R2009a')

gets the original path back.

relfile('C:\Program Files\MATLAB\R2009a\bin\matlab.exe','C:\Program Files\MATLAB\R2009a\toolbox\matlab')

returns '..\..\bin\matlab.exe' and

rel2fullfile('..\..\bin\matlab.exe','C:\MATLAB\toolbox\matlab')

expands to 'C:\MATLAB\bin\matlab.exe'

Note on OS Compatibility:
Although these functions are written to be OS independent, they have been only tested under Windows. Please report here if you successfully used these functions under Linux or Mac.

Comments and Ratings (5)

Igor

Igor (view profile)

And one more bug:

relfile('W:\A\','W:\A\B')==''

I've just noticed, that R2016a (preview) throws lots of:
--------------
Warning: NARGCHK will be removed in a future release. Use NARGINCHK or NARGOUTCHK instead.
> In relfile (line 27)
--------------
(which could be easily fixed, though).

Kesh, could you please update your submission, just to keep it up-to-date?

Kesh Ikuma

Kesh Ikuma (view profile)

@Igor - thanks for the report. I've uploaded the fixed version

Igor

Igor (view profile)

Just found another bug:

relfile('W:\temp\1.txt','W:\temp\')
returns "..\1.txt",

while

relfile('W:\temp\1.txt','W:\temp')
returns "1.txt" .

It also errors for
relfile('W:\temp\','W:\temp\')

Tal Kenig

Works fine on Linux.
Note that it works wrong if the 2nd input contains /..
Overall very handy.

Updates

1.9

- fixed a bug in RELFILE (erred out if FILE was a cellstr)

1.8

bug fix in relfile.m

1.7

fixed the bug reported by igor

1.6

Another little mistake corrected...

1.4

Corrected last-minute mistakes in previous submission

1.2

Complete revamp of the codes, fixing a few bugs along the way.

1.1

Added support for FILE given as a cellstr array, allowing multiple conversions with 1 call

MATLAB Release
MATLAB 8.1 (R2013a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video