0

Let me explain my situation with some dummy file names.

I am working in directory 'A' which has a sub directory 'a'. I am running a function 'func1' which is present in both folders. 'func1' needs 'file1' & 'file2' during its execution. 'file1' & 'file2' are present in both folders with some parameters changed inside them. It is not possible for me to change file names at all.

Now, the problem is that when I am running 'func1' in 'A', everything is working fine. But, when I run 'func1' in 'a' using 'addpath/rmpath', rather than using 'file1' & 'file2' from 'a', it is using 'file1' & 'file2' from 'A' which is producing wrong results.

Please tell me how can I change path so that when I run 'func1' in sub directory 'a', it always use 'file1' & 'file2' from 'a' rather than directory 'A'.

I hope I am clear in my explanation :S

1
  • When you say that you "use" file1 and file2, what do you mean? Are they .m files or are they data files that you open with load? Or fopen?
    – devrobf
    May 24, 2013 at 11:33

2 Answers 2

1

If I have understood correctly, you are hoping that if you use addpath to add the subdirectory to the search path, Matlab will give the search path precedence over the current directory. Unfortunately, it is precisely the other way around, as per the Matlab documentation: "Functions in the current folder take precedence over functions with the same file name that reside anywhere on the search path." - and this also applies to the load function when reading data files. (incidentally, I suspect that for this reason you are also not running the version of func1 that you think you are running - try typing which func1 to find out).

Anyway, the solution here is to make sure that Matlab picks the right version of file1 and file2, which you could do in several ways:

  • Change your working directory to a, since the working directory has precedence: cd a
  • Put the two versions into separate subfolders, e.g. a and b, and use addpath to add them separatley
  • Change the different versions of func1 to have explicit references to the files, i.e. load('./a/file1')
1
  • YUP!! 'cd' is the command looking for. Now with 'cd', when I refer to sub-folder 'a', I am able to run all files which present in folder 'a' no matter how many files are present in the main folder 'A' with the same name. Thanks mate!
    – Mushi
    May 24, 2013 at 12:39
0

With addpath and rmpath you modify the search path in Matlab. Your search path basically is a list of folders where Matlab looks for functions. Not for files you want to open.
If you have your files in folder A and this is your current working directory, Matlab will look for the files in A. If you change to a and change your working directory accordingly, Matlab will open the files in a - this has nothing to do with your search path. If you want to open files from a specific directory, use the entire path in the open command:

fileID = fopen('/path/to/A/file1');  

In your case, the case may be that the fopen is applied in the way explained above. If you want Matlab to always open files from the current working directory, change it to:

fileID = fopen('file1');  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.