folder path difference in 'Windows' and 'Linux' system

I found that the folder path expression is different between 'windows' and 'linux' system.
for window system, it use '\' between folders and sub-folders,
in linux system, it uses '/'.
this make some error when I translate the matlab code from windows to linux system.
is this an inherent feature of Matlab? or is there anyway to solve this problem?
Thansk!
Yu

 採用された回答

Rik
Rik 2019 年 1 月 13 日

1 投票

This is not an inherent feature of Matlab, but of the operating system that runs Matlab.
You can avoid this problem by not hardcoding your folder paths, and if you do, use fullfile and/or filesep.

5 件のコメント

Yu Li
Yu Li 2019 年 1 月 13 日
Thanks for your reply.
for the code developed in Windows system, I have code such as tmp=[pwd,'\test.mat'], but when I translate these code into Linux it reports error, linux does not recognize folder path such as : a/b/c/d\e, but windows does.
so looks I have to revise the source .m file one by one to address that.
Rik
Rik 2019 年 1 月 14 日
Glad to help. If it solved your problem, please mark my answer as accepted.
Walter Roberson
Walter Roberson 2019 年 1 月 14 日
Rik is correct that this is an operating system issue, and that we recommend using fullfile()
If you must hard-code path separators, then code them as / instead of as \ .
tmp = [pwd '/test.mat']
is valid on Windows, Mac, and Linux.
Windows uses / internally, but for historic reasons going way way back to DOS's origin as a CP/M clone, the presentation to users changes the / in paths to \ on output.
Stephen23
Stephen23 2019 年 1 月 14 日
"I have code such as tmp=[pwd,'\test.mat'], but when I translate these code into Linux it reports error"
This is exactly why you should avoid hard-coding file separators and avoid using string concatenation. It is much better to use fullfile, which automatically takes care of this.
This difference in separators has nothing to do with MATLAB, it is determined by the OS. Other OSs use other file separators too:
Yu Li
Yu Li 2019 年 1 月 14 日
Thank you all!
Bests,
Yu

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFilename Construction についてさらに検索

質問済み:

2019 年 1 月 13 日

コメント済み:

2019 年 1 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by