What is the search path? How do I use the 'path' command in MATLAB?

2 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2012 年 9 月 4 日
編集済み: MathWorks Support Team 2016 年 10 月 31 日
I would like to know how to use the 'path' command in MATLAB. I would like to know what the search path is, and how I can automatically add directories to the search path when I start MATLAB.

採用された回答

MathWorks Support Team
MathWorks Support Team 2016 年 10 月 31 日
The MATLAB search path is the list of directories in which MATLAB expects to find MATLAB files. When searching for a MATLAB file, MATLAB will search these directories in order, using the first one it finds.
For a complete discussion on the MATLAB search path, refer to the MATLAB documentation:
>> doc path
The current directory is always searched first, before any directories in the search path.
The MATLAB search path is set on startup. Under Unix, it is set by the "matlab" Bourne shell script which invokes the binary for your specific architecture. The MATLAB search path also always includes a directory called "matlab" under your home directory (if it exists). Under Windows, it is set in the MATLABRC.M file, which is invoked on startup. On the Macintosh, it includes all folders under the MATLAB folder.
You can add your own directories to the search path using the 'path' command. See "help path" for a review of the syntax. There are two notes about adding directories which are not necessarily obvious from the online help.
1) To add a directory to the search path automatically on startup, you can put the path command in a file called "startup.m". This file must always be available when MATLAB starts: it must either be in the directory in which MATLAB is started or in a directory already in the MATLAB path. Under Unix and Mac OS X, it is commonly placed in the matlab directory under your home directory. Under Windows, it is commonly placed either in C:\MATLAB\TOOLBOX\LOCAL, or in the directory which MATLAB starts in. This latter can be set by looking at the "properties" of the MATLAB startup icon shortcut or Start Menu item (Windows 95/NT right click the shortcut or Start Menu item).
As an example, the following might appear in your startup.m file:
 
path(path, '/home/john/mfiles'); % Unix, or
path(path, 'c:\matlab\mfiles'); % Windows
2) Macintosh users can manipulate the MATLAB search path using the File --> Set Path... menu item.
3) Unix users can access many important directories using the 'getenv' function. This will return environment variables, which will contain important directories. For example, to add your home directory to the MATLAB search path, you can use the following:
path(path, getenv('HOME'))
This can be useful in cases where automounting makes directory names look different depending on where you have logged in.
At the present time, the tilde ~ (commonly used as an abbreviation for the Unix home directory) does not work in setting the MATLAB search path.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by