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

1 Ansicht (letzte 30 Tage)
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)

Akzeptierte Antwort

Gavin
Gavin am 27 Sep. 2024
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.

Weitere Antworten (2)

Matt J
Matt J am 26 Sep. 2024
Bearbeitet: Matt J am 26 Sep. 2024
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 am 26 Sep. 2024
Bearbeitet: Image Analyst am 26 Sep. 2024
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

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by