Code covered by the BSD License  

Highlights from
Relative Filename


4.0 | 2 ratings Rate this file 13 Downloads (last 30 days) File Size: 4.97 KB File ID: #25701

Relative Filename



31 Oct 2009 (Updated )

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

| Watch this File

File Information

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.


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


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.

MATLAB release MATLAB 8.1 (R2013a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
27 Sep 2013 Kesh Ikuma

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

27 Sep 2013 Igor

Just found another bug:

returns "..\1.txt",


returns "1.txt" .

It also errors for

26 Jan 2011 Tal Kenig

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

19 Jan 2011

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

16 May 2013

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

17 May 2013

Corrected last-minute mistakes in previous submission

17 May 2013

Another little mistake corrected...

30 Sep 2013

fixed the bug reported by igor

17 Oct 2013

bug fix in relfile.m

04 Nov 2013

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

Contact us