Why does the "dos" command not work as expected when using "cd" to switch directories?
조회 수: 9 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2024년 11월 14일
편집: MathWorks Support Team
2025년 4월 11일
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"?
채택된 답변
MathWorks Support Team
2025년 4월 11일
편집: MathWorks Support Team
2025년 4월 5일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!