I am experiencing a problem when trying to use the Matlab system command 'cd' to change the current system directory. On my laptop, it works fine. However, on my university desktop machine (networked mac pro), it doesn't seem to want to work no matter what I do.
For example, say I start in the directory /test. I verify this by typing > !pwd , which tells the system to print the current directory.
If I then assign a matlab variable a path string e.g.
> datapath = '/anotherdirectory/test2'
and then try to change into it by typing
> cd(datapath)
When I do this on my laptop, !pwd returns /anotherdirectory/test2. However, on my desktop, !pwd returns /test. No matter what I do, cd will not change the system directory outside the Matlab environment on my work machine. I've tried all sorts of variants to get the cd command to work such as
> system(horzcat('cd ',datapath))
as well, but without success. This is becoming a major pain for me, because none of my data reduction scripts are going to function if I cannot get this functionality to work without MAJOR overhauls. Anyone got any ideas? Perhaps it is some sort of permissions issue with my work machine, where it won't allow another program to perform system commands or something?

 채택된 답변

Daniel Shub
Daniel Shub 2011년 11월 9일

2 개 추천

From your comment to Walter, it seems you are trying to change the directory of the "shell" from within MATLAB. It is not really clear what this means. What do you get if you do something like:
[a,b] = system('pwd; cd .. ; pwd')
This will be different then
!pwd
!cd ..
!pwd
The second case starts a new shell before and exits the shell after every command. The first case only create a single shell. How are you making your system calls?

댓글 수: 4

Craig Anderson
Craig Anderson 2011년 11월 10일
Thanks a heap Daniel - you've saved my world from imploding in on itself. Your first command does the trick nicely. Funny though that either set of commands works on my laptop though. I suspect that this is possibly due to the issue that Mr. Govind mentioned below...
Marco A. Acevedo Z.
Marco A. Acevedo Z. 2022년 10월 17일
Note that the system() function do not allow changing directory between different disks in the PC. You are trapped in either C: or D: and will need to do cd() function before and after the call to produce the desired effect. This is important because you wont be able to call external functions between paths (if in different disks) added to the same script.
system('cd /D D:\New\Path\Goes\Here; appropriate command goes here')
Marco A. Acevedo Z.
Marco A. Acevedo Z. 2022년 10월 18일
Thanks for the tip. My previous comment is not longer valid. Note that the ' /D ' represents the word 'another drive', not the letter of a different disk that follows up. :)
Cheers,

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 11월 9일

0 개 추천

The first thing I would suggest trying is
which -all cd
just in case you somehow have introduced your own cd.m file .
Also, what does pwd (without the '!') return in each case?

댓글 수: 4

Walter Roberson
Walter Roberson 2011년 11월 9일
And of course on the desktop I would check that /anotherdirectory/test2 exists on the machine, and doesn't happen to be a symbolic link to /test
Craig Anderson
Craig Anderson 2011년 11월 9일
Hi Walter,
Thanks for the reply.
- The directory exists and is not a symbolic link.
- which -all cd returns:
which -all cd
built-in (/Applications/MATLAB_R2010b.app/toolbox/matlab/general/cd)
/Applications/MATLAB_R2010b.app/toolbox/matlab/iofun/@ftp/cd.m % ftp method
-pwd without the bang shows that the command is at least changing the directory within Matlab. That is, using >cd(datapath) as per my original question and then typing >pwd returns '/anotherdirectory/test2'. cd('/') and then pwd again returns '/'. It seems to be changing the current directory in matlab without a problem, but not as far as the system itself goes.
The problem is that my data reduction script largely consists of a bunch of file handling routines, and wrapper function calls to miriad - a radio astronomy data reduction program. For miriad to put files in the correct place during the reduction, the system directory needs to be set to where I want the files to end up. Not being able to use cd means that this is a big problem considering the amount of data that I have to run through it!
Kaustubha Govind
Kaustubha Govind 2011년 11월 9일
Does your shell startup script always force the shell to start in a certain directory? I've seen that this has been the most likely cause for these issues in the past. One good way of testing this is to start a new shell window from within MATLAB and check if it always opens in a certain directory. I'm not familiar with Mac, but on Windows, you would use something like "!start &" to start a new system command window.
Craig Anderson
Craig Anderson 2011년 11월 10일
Thanks for that! I'll go and check this out too. Much appreciated!

댓글을 달려면 로그인하십시오.

Konrad Gromaszek
Konrad Gromaszek 2018년 9월 13일

0 개 추천

Hello,
On my Mac code I've used
path_str = './MLP';
cd(path_str);
instead of
cd('./MLP');
... and it works!

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2011년 11월 9일

댓글:

2022년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by