File Exchange

image thumbnail


version (2 KB) by Jochen Lenz
Convert path representations.


Updated 18 Aug 2003

View License

RELATIVEPATH returns the relative path from an actual path to the target path.

Both arguments must be strings with absolute paths. The actual path is optional, if omitted the current dir is used instead. In case the volume drive letters don't match, an absolute path will be returned.

If a relative path is returned, it always starts with '.\' or '..\'

rel_path = RELATIVEPATH( target_path, actual_path )

Comments and Ratings (12)

Sam Walder

Yul Kang

The following modification removes '\' at the end:

% Relative directory levels to target directory:
rel_path = fullfile(rel_path, tgt_path_cell{:});
% for i = 1 : length(tgt_path_cell)
% rel_path = [rel_path tgt_path_cell{i} filesep];
% end


M. Luppi


One more bug - this function alway adds "\" on to the end of the result:

relativepath( 'c:\temp\1.txt', 'C:\temp') = '.\1.txt\'


results in lowercase letters...


David Zhang

works great on mac, does it also work on windows?

David Zhang

David Zhang

Ano Nymous

worked fine, good examples

Changshun Deng

very good! many thanks!

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