4

It happens that your application encounters a directory path that is relative to current folder or uses double dot for navigation, e.g. C:\A\B\..\C. This obviously is equivalent to the canonical path C:\A\C. How can one resolve the path to its canonical form?

3 Answers 3

8

This one uses the java io interface:

jFile=java.io.File(iPath);
oPath=jFile.getCanonicalPath;   

It wouldn't need to change matlab's directory. It has other useful methods that may be found here.

Sign up to request clarification or add additional context in comments.

Comments

4

The simplest way I know to convert a path to its canonical form is using cd command:

oPath = cd(cd(iPath));

Note that this would fail if the path does not exist on your file system.

Comments

0

Another way of doing it, without potentially throwing an exception, is using the what command:

pathInfo = what(iPath);
if ~isempty(pathInfo)
    iPath = pathInfo.path;
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.