Path, Current Directory, "exist" and "which" functions

7 views (last 30 days)
Hi
Here is my problem
If we consider two files whose path are :
  • studies_1\data\file.mat
  • studies_2\data\file.mat
were "studies_1" and "studies_2" are in the Matlab path, but not "studies_1\data" or "studies_2\data".
If Current Directory is "studies_1" or"studies_2" , following command :
exist('data\file.mat','file')
will return 2 (the file exist), but :
which('data\file.mat')
will return 'data\file.mat' not found.
So, how can I know exactly which file among "studies_1\data\file.mat" and "studies_2\data\file.mat" is considered by functions like exist or load when I use "data\file.mat" ?
Does it ONLY depend on the Current Directory?

Answers (2)

Junaid
Junaid on 2 Dec 2011
Dear I guess it depends on your current directory. Let say you are in studies_1/ directory. then
load ('data/file.mat');
this will load the file in studies_1/data. However in my system I can easily check
which('data/file.mat');
you try with both, forward and backward slashes. If you have further confusion then better to give full path in load time.

Walter Roberson
Walter Roberson on 2 Dec 2011
The documentation for which says,
which file.ext displays the full pathname of the specified file if that file is in the current working folder or on the MATLAB path. To display the path for a file that has no file extension, type "which file." (the period following the filename is required). Use exist to check for the existence of files anywhere else.
Thus, "which" is not suitable for determining whether a file exists in a subdirectory.

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!