Code covered by the BSD License  

Highlights from
realpath

3.5

3.5 | 2 ratings Rate this file 3 Downloads (last 30 days) File Size: 1.63 KB File ID: #26360

realpath

by Anthony Corr

 

12 Jan 2010

Resolves relative paths and extra filesep characters in the input.

| Watch this File

File Information
Description

Updated version of realpath by M MA using suggestions made on that file submission and additional testing. Works for Windows, may need some tweaks for Unix. Substantially faster than original version in testing.

Resolves relative paths and extra filesep characters in the input.

Syntax:
OUTPUT = REALPATH(THEPATH)

where THEPATH is the path to a file or folder, which should exist.

Example:
realpath('../myfile.txt');
% returns /home/user/whatever/myfile.txt

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
realpath

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
13 Jan 2010 John D'Errico

This worked in my tests on a Mac. Good help. An H1 line.

I generally don't worry about paths to a file or directory, or how absolute they might be. But if that is an important fact for you, this utility might be useful.

24 Jul 2010 Jan Simon

- The file or folder *must* exist, not just *should*, as stated in the help section.
- REALPATH replies a file in the current directory, if the file name in an existing toolbox or userdefined function, although the file does not exist in the current folder: realpath('plot.m');
- 2 of these 3 calls are enough to get the needed information: "exist(thePath,'file') == 2",
"fileattrib(thePath)" and "isdir(thePath)".
- "output(end:end)" is the same as "output(end)".
- REALPATH changes the current folder. This disturbs the strange handling of e.g. "C:" under Windows. "C:" is the last used current folder on the disk C, while "C:\" is the disk C itself. Try it:
  cd(matlabroot); cd('D:\'); dir('C:'); dir('C:\');
(assuming that Matlab is on disk C). Therefore the trailing file separator should not be removed for disks.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
path Anthony Corr 13 Jan 2010 12:25:56
absolute path Anthony Corr 13 Jan 2010 12:25:56
relative path Anthony Corr 13 Jan 2010 12:25:56

Contact us at files@mathworks.com