View License

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

» Watch video

Highlights from
Relative Filename

3.0 | 3 ratings Rate this file 4 Downloads (last 30 days) File Size: 4.97 KB File ID: #25701 Version: 1.9

Relative Filename


Kesh Ikuma (view profile)


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 (5)
13 Feb 2016 Igor

Igor (view profile)

And one more bug:


30 Dec 2015 Igor Varfolomeev

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?

Comment only
27 Sep 2013 Kesh Ikuma

Kesh Ikuma (view profile)

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

Comment only
27 Sep 2013 Igor

Igor (view profile)

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 1.1

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

16 May 2013 1.2

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

17 May 2013 1.4

Corrected last-minute mistakes in previous submission

17 May 2013 1.6

Another little mistake corrected...

30 Sep 2013 1.7

fixed the bug reported by igor

17 Oct 2013 1.8

bug fix in relfile.m

04 Nov 2013 1.9

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

Contact us