Why does my matlab current working sometimes differ from the system's current working directory after using cd?

21 views (last 30 days)
Hi,
I'm noticing an odd issue where the system's current working directory is not correctly updated by the cd command in Matlab.
Example:
>> cd /data/mridata/test/11000/022013
>> pwd
ans =
/data/mridata/test/11000/022013 ----DIFFERS
>> unix('pwd')
/data/mridata/alldata ----DIFFERS
This causes major issues with any of the matlab builtins that can work on the current working directory (ex. ls, load, open, etc). For example, calling ls produces the file list for the directory output by unix('pwd') and not matlab's current working directory, displayed in the address field or current folder listing.
Matlab Version: R2013a Linux Version: Ubuntu 12.04 LTS
Thanks for any advice!

Answers (1)

Walter Roberson
Walter Roberson on 17 Apr 2014
unix('pwd') is going to invoke the command pwd, which is going to use the Unix ftw() call to climb the directory tree until it reaches the root. As it climbs, it is going to read the parent directory of the directory it is examining, looking to see which name in the directory references the inode number (and drive) of the directory it is examining. The path that is created will therefor always be completely composed of "hard" directory names, on the shortest route from the root to the directory.
MATLAB pwd, on the other hand, is going to use the shell PWD environment variable to determine the name of the directory it starts in, and is going to use the user names for directories, making any necessary relative adjustments as the user gives them.
The two approaches differ if any of the directories are soft links. unix('pwd') is going to give "hard" names only, but MATLAB pwd (and shell pwd) are going to manage by name instead of by hard path. So if you cd /a/b/c in MATLAB and c is a softlink to /e then unix('pwd') would tell you /e (the "real" directory name) but MATLAB (and shell) pwd would use the /a/b/c that you gave.
  1 Comment
Andrew
Andrew on 17 Apr 2014
Thanks for the suggestion. In my experience with this issue so far I don't think I have been trying to access softlinks. In the revised example above, '/data/mridata/alldata' and '/data/mridata/test/11000/022013' only share the parent directory '/data/mridata' but are not linked in any other way.
What brought this to my attention initially was an attempt to change directory using cd and then immediately printing the contents using ls. I noticed that the printed contents differed from what was listed in the 'Current Folder' panel in the GUI. I then compared the outputs of pwd and unix('pwd') and it turned out that ls was listing the contents of the the directory resulting from unix('pwd'), a completely different and folder I was in prior to issuing the cd command. Interestingly, I was able to fix the issue in that instance by issuing the same exact cd command. Finally the results of pwd and unix('pwd') matched and printing the directory contents using ls worked as expected. It seemed that the cd command failed to update the system's current working directory so when I listed the contents I was getting the contents of the previous working directory.

Sign in to comment.

Categories

Find more on Search Path in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!