How to resolve relative directory path to canonical path in MATLAB / Octave

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

+4
source share
3 answers

The easiest way I know to convert the path to its canonical form is the cd command:

 oPath = cd(cd(iPath)); 

Please note that this will not work if the path to your file system does not exist.

+4
source

This interface uses the java io interface:

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

No need to change the Matlab directory. It has other useful methods that can be found here .

+6
source

Another way to do this without the potential exception exception is to use the what command:

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

Source: https://habr.com/ru/post/1498543/


All Articles