MATLAB解析特定于操作系统的路径

编程入门 行业动态 更新时间:2024-10-13 16:21:14
本文介绍了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解析特定于操作系统的路径

本文发布于:2023-11-14 20:05:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   操作系统   MATLAB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!