如何定义自定义目录以便我可以在 Matlab 中 cd 目录

How to define a custom directory so that I can cd directory in Matlab

假设我有一个目录

cur = 'C:\Windows\debug';

那我现在可以运行cd(cur)了。但是我不习惯使用函数格式。我希望我可以使用 cd cur 直接更改当前文件夹。这在 MATLAB 中可行吗?

编辑:因为我收到以下错误:

>> cur = 'C:\Windows\debug';
>> cd cur
Error using cd
Cannot CD to cur (Name is nonexistant or not a directory).

这里是 documentation for command syntax, and a documentation article with more examples on command vs function syntax.

根据文档,

When calling a function using command syntax, MATLAB passes the arguments as character vectors.

所以不,你不能传递像 cur 这样的变量名,因为 cur 将被视为字符向量,你将做与 cd('cur') 相同的事情。

你可以做任何一个

cd(cur)
% or
cd 'C:\Windows\debug'
% or (as long as no whitespace in directory path)
cd C:\Windows\debug

如果您不喜欢学习语法,解决方法是选择另一种语言...使用括号是 MATLAB 中的标准做法,因为在使用命令语法时您也无法从函数中获取输出值。

也可以从scripts and functions documentation看到消息

Caution: While the unquoted command syntax is convenient, in some cases it can be used incorrectly without causing MATLAB to generate an error.

所以在使用 MATLAB 时不鼓励使用这种方法。