What does “shadows it in the MATLAB path” mean? Ho

2019-04-27 12:55发布

问题:

I need to do this always before running unitTester file. I cannot understand why this is required. What does this mean? And why is the "Add to Path > Selected Folders and Subfolders" not enough?

[Update] This here may be the problem. The kernel is programmed in different techniques where the file names are the same. I need to make sure only certain files are used in each case. Is the easiest way use here a package not to mess up namespaces?

回答1:

MATLAB searches for m-files on its search path, you can display it using the path command. If you have multiple m-files with the same basename (i.e. the part of the filename before the extension, excluding the directories) on the MATLAB path then MATLAB can only execute the one that comes first on the path.

You can use the addpath and rmpath functions to dynamically modify the path. For example, you could automatically add the relevant directories automatically in your test running script. Note that addpath adds the new path to the head of the path list, which makes sure that it takes precedence over the existing entries.

Another way to prevent name conflicts like these are packages.

EDIT: To convert a directory into a package, do the following:

  • Add a + at the beginning of its name.
  • Make sure you put the directory's parent onto the MATLAB path.
  • Update all calls to functions within the package by either prepending packagename. to them or by including the package contents before the calls (import packagename.*).

In general I'd prefer packages to dynamic path modifications, because they're easier to use. Note that you can nest packages (i.e. my_matlab_files/+mypkg/+nested/foo.m).



回答2:

I encountered the same problem on OS X 10.10.3 with Matlab r2015a.

I had my files in ~/electrochemistry/Matlab/

Somehow renaming the last folder to lowercase resolved the problem. While troubleshooting I noticed Matlab was looking in ~/electrochemistry/matlab/, but couldn't find what it was looking for (i.e. my .m-file I was running)

So now I'm working in ~/electrochemistry/matlab/ and the problem is solved