MATLAB解析特定于操作系统的路径 [英] MATLAB parse OS-specific path

查看:122
本文介绍了MATLAB解析特定于操作系统的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个MATLAB项目,该项目由多个用户共享,一些用户运行Windows,一些用户运行Linux.

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

在某些脚本中,我需要访问外部目录中的文件,并且这些文件我不想添加到MATLAB路径中.

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.

要同时适应Linux和Windows,我必须能够确定我正在运行的操作系统的类型,并相应地设置目录分隔符(对于Windows为'\',对于Linux为'/').

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).

我尝试了

os = getenv('OS')

(我在一些官方指南中看到过),但是它返回一个空字符串.

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

我可以检查'pwd'的第一个字符,但这很丑陋,我希望应该有一些更简单的东西.

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

谢谢您的建议!

推荐答案

要使用正确的目录分隔符,您无需编写代码即可处理不同的操作系统. filesep为您提供正确的目录分隔符.

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 ];

在Linux中,您将获得:

In Linux you'll get:

MyDir =
     mydata/Year2012/Feb/Day03

在Windows中,您将获得:

In Windows you'll get:

MyDir =
     mydata\Year2012\Feb\Day03

这篇关于MATLAB解析特定于操作系统的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆