Why does the "dos" command not work as expected when using "cd" to switch directories?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 14 de Nov. de 2024
Editada: MathWorks Support Team
el 11 de Abr. de 2025
The "dos" command does not work as expected for switching directories with the "cd" command.
>> [status, cmdout] = dos('cd C:\')
cmdout =
0×0 empty char array
However, the "dos" command works for different operations like "dir" fine.
>> [status, cmdout] = dos('dir')
cmdout =
' Volume in drive C is Windows
Volume Serial Number is 6405-C0AD
Directory of C:\Users\..\...\...\...
12/26/2024 04:35 PM <DIR> .
12/30/2024 02:49 PM <DIR> ..
Why does the "dos" command work with "dir" but not with "cd"?
Respuesta aceptada
MathWorks Support Team
el 11 de Abr. de 2025
Editada: MathWorks Support Team
el 5 de Abr. de 2025
The behavior you are observing is expected. When you use the "dos" command to execute a command like "cd", it runs in a separate command shell instance. This means that any directory changes made using "cd" within that shell do not affect the MATLAB environment itself. Essentially, each "dos" command operates in its own isolated shell session.
For changing the current working directory within MATLAB, you should use MATLAB's built-in "cd" function. Here’s how you can do it:
cd('C:\path\to\your\directory');
This command will change the directory for the MATLAB session, allowing any subsequent file operations to occur in the new directory.
However, you can execute command-line operations like listing directory contents using "dos", which does not affect MATLAB's current directory, like "dir":
[status, cmdout] = dos('dir');
disp(cmdout);
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!