File Exchange

image thumbnail

Relative Filename

version (4.97 KB) by Kesh Ikuma
A pair of M-files to convert between full and relative filenames.


Updated 04 Nov 2013

View Version History

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.


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.

Cite As

Kesh Ikuma (2021). Relative Filename (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)


And one more bug:


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?

Kesh Ikuma

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


Just found another bug:

returns "..\1.txt",


returns "1.txt" .

It also errors for

Tal Kenig

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

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!