dealing with \ and / (windows vs. unix) for path definition.

조회 수: 81 (최근 30일)
pieter vandromme
pieter vandromme 2014년 2월 21일
편집: Stephen23 2024년 8월 8일
Hi everybody,
Simple question, let's say I'm using mac osx and some colleagues use windows... and I have to share a matlab script. Problem, this script needs to look in folder to load data files or print figures:
load ./data/data.mat
plot(data(:,1),data(:,2))
print -dpdf ./graph/figure.pdf
this is how it is written on unix system, but on windows I have to replace all the / by \
load .\data\data.mat
...
Is there a way to deal with that or the only way to make it works on both systems is to add a conditional statement like
if isunix load ./data/data.mat else load .\data\data.mat end
thanks for any help,
cheers
Pieter

채택된 답변

Image Analyst
Image Analyst 2014년 2월 21일
Matt's right. You can use forward slashes when making up path strings under Windows versions of MATLAB. It handles them just fine. No need to ever use backslashes. Actually you don't even need filesep. This works just fine as a path:
myMFilesPath = 'D:/MATLAB/work/my m-files'; % Perfectly fine under Windows.
  댓글 수: 2
Ankit Labh
Ankit Labh 2024년 8월 8일
I must say that this is not working. If I have a file path in windows, it is simply not working for mac and vice versa.
An example is the following:
cd ':\Users\E_Drive_OD\Folder1'
This is how one goes to the Folder1 directory, but each time I use matlab in mac, I have to give Mac like directory address, which is the following:
cd '/Users/E_Drive_OD/Folder1'.
Can you suggest me a simple way to convert the folder directory address compatible with both? I am looking for something like this:
magic_mac(windows_path) % converts windows path to mac path
magic_win(mac_path) % converts mac path to windows path
Thank you.
Stephen23
Stephen23 2024년 8월 8일
편집: Stephen23 2024년 8월 8일
"I must say that this is not working"
Your Windows example shows that you are using backslashes, not forward slashes like this answer advises. Forward slashes are accepted on Mac, Linux, and Windows. This is explained in the MATLAB documentation: "A forward slash (/) is a valid separator on any platform"
If you are using relative paths (i.e. using . or .. on the leftmost end of the path) then forward slashes will work with one path on all of those platforms.
Of course how those OS's refer to drives and various special folders is completely different, so if you want one absolute path that will work on all platforms then you are out of luck.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Matt J
Matt J 2014년 2월 21일
편집: Matt J 2014년 2월 21일
You can use the FILESEP command. However, are you sure it's necessary? I always seem to remember MATLAB being smart enough to parse directory path strings correctly regardless of which separator you use.

pieter vandromme
pieter vandromme 2014년 2월 21일
편집: pieter vandromme 2014년 2월 21일
Hi, thanks for your answer,
The command FILESEP works fine.
I've made also some try to see if matlab was reinterpreting the path according to the OS in a clever way but it seems not to work. Yet, with the Image analyst answer it seems to work only in one way. / -> \, or when a unix path is interpreted on windows, but not the opposite! So, it is good to know that we have to write the path always with / and never with the windows way!
cheers and thanks again.
  댓글 수: 1
Image Analyst
Image Analyst 2014년 2월 21일
편집: Image Analyst 2014년 2월 21일
I'm not sure what the middle sentence means, but your final sentence that says you can always use /, and never use \, regardless of platform/OS is correct.

댓글을 달려면 로그인하십시오.


jim bob
jim bob 2020년 3월 2일
Sometimes a function (such as unzip) will return filenames in the format appropriate for the platform you are using so you may need to convert from one system to another.
path_pc = '\path\to\folder';
%convert to a unix version:
path_unix = path_pc;
path_unix(strfind(path_unix,'\'))='/';

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by