issue with using a variable for path name

I tried to save my path address to a variable and use the variable as CD funtion but i am getting an error.
Path = ('H:\dcx_cpe\CRANE\Truck Data');
cd('Path');
I am getting the below error.
Cannot CD to H:\dcx_cpe\CRANE\Truck Data\Path (Name is nonexistent or not a directory).
How do i fix this issue?

1 comentario

Stephen23
Stephen23 el 7 de Abr. de 2022
Do not CD just to import/export data files. CD is slow and makes debugging harder.
Every function that import/exports datafiles accepts absolute/relative filenames. You should use absolute/relative filenames.

Iniciar sesión para comentar.

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 7 de Abr. de 2022

0 votos

cd(Path).
avoid using 'Path' as variable name as path() is a function.

7 comentarios

sri satya ravi
sri satya ravi el 7 de Abr. de 2022
Thq. that helps.
sri satya ravi
sri satya ravi el 7 de Abr. de 2022
Hi,
Anyidea why matlab is not setting the path/current folder with this condition?
it is basically not changing the folder to the below directory which is why it is throwing an error.
Directory = cd("H:\dcx_cpe\CRANE\Truck Data");
cd (Directory)
Fangjun Jiang
Fangjun Jiang el 7 de Abr. de 2022
Probably something related to your computer. Do some experiments on the known folder that you can go to, either through Windows Explorer, or manual navigation in MATLAB by clicking the buttons, and then try the cd() function.
Steven Lord
Steven Lord el 7 de Abr. de 2022
The output of a call to cd is the directory that was the current directory before MATLAB changed to the new directory. So your first line changes to the new directory and your second line changes right back to the previous current directory.
This behavior gives you a way to restore the previous current directory (using an onCleanup object, for instance) in your code to minimize the amount of changes your code makes to the state of MATLAB. If you did use an onCleanup in a function then when the function exits (whether it exits normally or exits because it threw an error) whatever action the onCleanup object is supposed to perform would be performed.
sri satya ravi
sri satya ravi el 7 de Abr. de 2022
Steven,
Can you please tell me what changes i need to make to the code.
Steven Lord
Steven Lord el 7 de Abr. de 2022
If you need to change the current directory, change it once not twice. That's like walking through a door then immediately walking through that same door again -- you end up where you started.
sri satya ravi
sri satya ravi el 8 de Abr. de 2022
Steven,Thanks. That helps.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 7 de Abr. de 2022

Comentada:

el 8 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by