Why did CD folder; give such a goofy error message when I just forgot ()

2 次查看(过去 30 天)
It works other places but suddenly I can change directory
K>> apploc
apploc =
'C:\Users\CPSLab\Documents\OdorChoiceDev\'
K>> cd apploc
Error using cd
Unable to change current folder to 'C:\Users\CPSLab\Documents\OdorChoiceDev\apploc' (Name is
nonexistent or not a folder).
Why did it add the variable name to the value of the variable instead of just reminding me I forgot the parentheses
cd (apploc)

采纳的回答

Gavin
Gavin 2024-9-27
All good answers and I would have retracted the question once I noticed I'd forgotten the ()
Of course cd (apploc) is what I wanted, as I had done before when it worked.

更多回答(2 个)

Matt J
Matt J 2024-9-26
编辑:Matt J 2024-9-26
Because
cd apploc
is equivalent to,
cd('apploc')
This is a general thing in Matlab function-calling syntax. For any function func() taking char vector arguments, the syntax,
func('arg1','arg2','arg3',...)
is equivalent to,
func arg1 arg2 arg3 ...

Image Analyst
Image Analyst 2024-9-26
编辑:Image Analyst 2024-9-26
Since apploc is a variable that contains the actual folder name, you need to use the functional form of cd:
cd(apploc);
If you don't, and omit the parentheses, then it tries to find a folder actually called apploc which does not exist, hence the error.
As a further explanation, if actually did have a folder called MyData and it was a subfolder of your current folder, let's say the current folder was d:\work, then
cd MyData
would change the current folder to d:\work\MyData. It would work in that case. If, unwisely, you also had a variable called MyData and it was a string variable with the value 'My images', then it would still change the current folder to d:\work\MyData since it would use the exact thing you typed (because you did not use parentheses) rather than replacing it with the value of the variable. So it would not change the current folder to 'd:\work\My images'.
But you should not even be using cd anyway. There are few good reasons to use it. You should be constructing full file names using fullfile rather than using cd. See the FAQ:
help fullfile
fullfile - Build full file name from parts This MATLAB function builds a full file specification from the specified folder and file names. Syntax f = fullfile(filepart1,...,filepartN) Input Arguments filepart1,...,filepartN - Folder or file names character vectors | string arrays | cell arrays of character vectors Output Arguments f - full file specification character array | string array | cell array of character vectors Examples openExample('matlab/CreateAFullFileNameExample') openExample('matlab/CreateAFullFilePathOnUNIXExample') openExample('matlab/CreatePathstoMultipleFilesExample') openExample('matlab/CreateaPathtoaFolderExample') See also fileparts, filesep, path, pathsep, genpath, split Introduced in MATLAB before R2006a Documentation for fullfile doc fullfile

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by