MATLAB parse OS-specific path

我怕爱的太早我们不能终老 提交于 2019-12-23 23:06:09

问题


I am running a MATLAB project, which is shared by several users, some running Windows and some running Linux.

In some of the scripts, I need to access files which are in external directories, and which I do not want to add to the MATLAB path.

To accommodate both Linux and Windows, I need to be able to determine the type of OS I'm running, and to set the directory separator accordingly ('\' for Windows, '/' for Linux).

I tried

os = getenv('OS')

(which I saw in some official guide),but it returns an empty string.

I could check the first character of 'pwd', but that's pretty ugly, and I expect that there should be something simpler.

Thanks for any suggestions!


回答1:


To use correct directory separator you don't need to write code to handle different operating systems. filesep gives you the correct directory separator.

My1stDir = 'Year2012';
My2ndDir = 'Feb';
My3rdDir = 'Day03';

MyDir = [ 'mydata', filesep, My1stDir, filesep, My2ndDir, filesep, My3rdDir ];

In Linux you'll get:

MyDir =
     mydata/Year2012/Feb/Day03

In Windows you'll get:

MyDir =
     mydata\Year2012\Feb\Day03


来源:https://stackoverflow.com/questions/10760327/matlab-parse-os-specific-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!