开发者

How to use the MATLAB search path

I have some questions regarding the MATLAB Search Path:

  1. The current directory is on the search path, but why is it not in the output of "path"? Where can I find the complete search path?

  2. What are all the possible ways to add search paths?

  3. Considering all the possible ways to add search paths (e.g. pathdef.m, startup.m, MATLABPATH env variable, etc), what is the order of the search paths added? I think it is important because when files with same name exist in different search paths, the one on the开发者_StackOverflow中文版 top will be picked.


The links provided by Amro should be quite helpful in answering your questions. To address them more specifically:

  1. The output from PATH will show the contents of the pathdef.m file, which should include all of the following:

    • Folders provided with MATLAB and other MathWorks products (i.e. toolboxes). These folders are located in the root MATLAB folder, which you can find using the MATLABROOT function.

    • The MATLAB user folder (i.e. My Documents\MATLAB on Windows platforms), which can be found using the USERPATH function.

    • Any other folders the user(s) has added to the path file.

    The complete search path contains the above, plus whatever the current directory is. The current directory isn't saved as part of the path file since it can be changed during the MATLAB session. You can find the current folder using the PWD function.

  2. The search path can be changed by changing either the path file or the current directory. You can modify the path file in the following ways:

    • Using the PATH function, along with the other associated functions ADDPATH, RMPATH, and SAVEPATH.

    • Using the Set Path dialog box, which can be opened from the Command Window by selecting File > Set Path or by calling the function PATHTOOL.

    • Modifying the startup.m file.

    And the current directory can be changed in the following ways:

    • Using the CD function.

    • Using the Current Folder Browser.

  3. When you modify the path file using the above methods, new folders are typically added to the top of the path list. You can change the order of the paths in the path file using the Set Path dialog box.

    When there are functions that share the same name, MATLAB follows the following function precedence order to determine which function to use:

    • Variable (if a variable shares the same name as a function)

    • Nested function

    • Subfunction

    • Private function

    • Class constructor

    • Overloaded method

    • Function in the current directory

    • Function elsewhere on the search path

    Note that a function in the current directory is called before one elsewhere on the search path. Also, files closest to the top of the search path have precedence over files further down.


The best answer is to point you to the relevant MATLAB documentation:

  • Using the MATLAB Search Path
  • Making Files and Folders Accessible to MATLAB
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜