2017-07-18 86 views
-1

光盤目錄假設我有一個目錄如何定義自定義目錄,這樣我可以在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). 
+0

要「cd」到包含在變量中的目錄,正如您嘗試的那樣,您必須使用功能表單。在你的例子中,MATLAB正在尋找名爲'cur'的目錄。 –

+0

@PhilGoddard當然我知道,但我仍然想這樣做。是否有任何解決方法? – yode

+2

引用「命令與函數語法」的文檔:當函數輸入是變量時,必須使用函數語法將值傳遞給函數。命令語法總是將輸入作爲文字文本傳遞,並且不能傳遞變量值。我不相信有一個解決方法。 –

回答

2

這裏是documentation for command syntax,並與更多的例子在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時氣餒。

+0

[That works](http://i.stack.imgur.com/e4uyC.png)給你? – yode

+0

我非常懷疑[該命令](http://i.stack.imgur.com/3Nq2D.png)可以繼續工作,而'cd(cur)'在這裏正常工作。 – yode

+0

我已經更新了我的回答 – Wolfie

相關問題