请输入搜索信息

HPC技术文章

一、问题描述
MATLAB使用搜索路径来查找.m文件及其他相关文件。任何你想运行的文件必须保存在当前目录或保存在处于探索路径上的目录中。默认情况下,每次启动MATLAB时,都使用位于matlabroot\toolbox\local目录中的pathdef.m定义搜索路径。
在这多用户的情况下,有一个潜在的问题——不同的用户使用不同的工具包,这些工具包可能有同名的.m文件,那么只有处于搜索路径前面的.m文件会被执行,另一个.m文件,永远不会被执行。
二、解决方法
(一)在本机临时更改搜索路径
将你所需的同名的M文件放到搜索路径的最前面
path('',path)
如果原先已经存在于搜索路径中,则该目录会被移至搜索路径最前面。
如果要在搜索路径中包含指定目录的子目录,可以先使用genpath得到所有子目录,再用path命令,如path(genpath('z:\yuan\documents\matlab'),path)。
或者是删除造成同名的另一个工具包路径
if findstr('< 造成同名的另一工具包路径>',path)
rmpath('< 造成同名的另一工具包路径>')
end
以上更改并没有保存下来,MATLAB进程关闭后重启,将恢复默认搜索路径。
(二)在本机自定义搜索路径
1.通过pathdef.m
如果pathdef.m在MATLAB的启动目录中(Startup Folder),则MATLAB通过启动目录中的pathdef.m定义搜索路径。因此当我们将matlabroot\toolbox\local中的pathdef.m拷贝到启动目录中,启动MATLAB进程,单击File-->Set path,根据需要设置并保存。
关于Startup Folder的信息,请参见“三、参考阅读”部分。
2.通过startup.m
可以通过startup.m指定启动选项,如可以在startup.m中加入修改路径的相关语句,然后将startup.m放到Startup Folder中,或放到搜索路径中的任何一个文件夹中。
 
如果你需要在本机同时启动一个以上MATLAB进程,且每个进程都有不同的自定义搜索路径,则可以先建立多个启动MATLAB的快捷方式,然后修改其Startup Folder
然后将pathdef.m放到每个进程指定的Startup Folder中。双击每个快捷方式,启动MATLAB后,再通过File-->Set path修改各自的搜索路径并保存。
或者将自定义的startup.m放到指定的Startup Folder中。
(三)在worker或lab上自定义搜索路径
当进行分布式或并行计算时,计算任务可能提交到其他计算节点,这时自定义搜索路径的方法至少有以下几种方法:
1.通过jobStartup.m
将matlabroot\toolbox\distcomp\user中的jobStartup.m拷贝到作业的PathDependencies属性所指定的目录中,然后使用修改路径的相关语句,如:
path('',path) 将你所需的同名的M文件放到搜索路径的最前面;
rmpath('< 造成同名的另一工具包路径>')  删除造成同名的另一个工具包路径。
注:在我们的HPC系统中,所有节点(登录节点和计算节点)的MATLAB默认搜索路径都是相同的。
2.通过设定作业的PathDependencies属性
PathDependencies所指定的目录会自动加入Worker搜索路径的最前面。
作业的PathDependencies默认指向Z:\User\Documents\MATLAB\,且它是一个1*1的Cell Array。
如何将其他目录加入到PathDependencie列表?
方法一:下面的语句将Z:\User\Documents\MATLAB中的Other目录添加到PathDependencie列表中
j.pathdependencies=[j.pathdependencies;'Z:\User\Documents\MATLAB\Other']
之后,j.PathDependencie就是2*1的Cell Array了。
● 方法二:下面的语句将Z:\User\Documents\MATLAB及其子目录替代原有的PathDependencie列表
p=textscan(genpath('Z:\User\Documents\MATLAB'),'%s','delimiter',';');
j.pathdependencies= p{1,1};
3.在计算程序(.m)中直接修改搜索路径
在每个task所指定的.m文件中,直接加入addpath或rmpath等相关命令修改搜索路径。
三、参考阅读
(一)Function Precedence Order
1. Variable
2. Nested Function
3. Subfunction
4. Private function
5. Class constructor
6. Overloaded method
7. MATLAB function file in the Current Folder
8. MATLAB function file on the path, or MATLAB built-in function
For items 4 through 8 in this list, the file MATLAB searches for can be any of four types: a built-in file or program file with a .m file extension, preparsed program file (P-Code), compiled C or Fortran file (MEX-file), or Simulink® model (MDL-file).
Multiple Implementation Types
There are five file precedence types. MATLAB uses file precedence to select between identically named functions in the same folder. The order of precedence for file types is
1. Built-in file
2. MEX-files
3. MDL (Simulink® model) file
4. P-code file
5. Program file with a .m file extension
(二)current folder、startup folder、userpath
1. current folder
即当前目录
2. startup folder
(1)什么是startup folder
The startup folder is the current folder in the MATLAB application when it starts. It is convenient if you make the current folder upon startup be a folder that you frequently use. On Windows and Apple Macintosh platforms, a folder called userpath is added automatically to the search path upon startup, and is the default startup folder.
The startup folder on Windows platforms depends on any startup options you specified and the way you started MATLAB:

 

How Started
Startup Folder
Double-click the MATLAB shortcut on your Windows desktop
The startup folder is set to the userpath value, whose default value is My Documents\MATLAB, or Documents\MATLAB on Windows Vista platforms. The userpath folder is automatically added to the search path. If there is a value specified in the Start in field of the Properties dialog box for the MATLAB program, that value is the startup folder, although the userpath is added to the search path. If MATLAB does not find a valid userpath value upon startup, and the Start in field is empty, the startup folder is the Windows desktop.
Double-click a file type associated with MATLAB
The folder in which the file resides is the startup folder. The userpath folder is automatically added to the search path.
In a DOS window
The folder in which you ran the command is the startup folder. The userpath folder is automatically added to the search path.

 

(2)更改startup folder
①通过userpath函数修改
默认情况下,userpath即是startup folder,所以可以通过userpath来更改。一般不建议修改userpath。
②在Windows平台下,通过快捷方式属性修改。
右键单击快捷方式,选“属性”,打开如下对话框,将“起始位置(start in)”改为另外一个目录,如Z:\User\Documents\MATLAB\Test。默认的起始位置是Z:\User\Documents\MATLAB。
③通过startup.m文件修改
At startup, MATLAB automatically executes the file matlabrc.m and, if it exists, startup.m. The file matlabrc.m, which is in the matlabroot/toolbox/local folder, is reserved for use by MathWorks and by the system manager on multiuser systems.
The file startup.m is for you to specify startup options. For example, you can modify the default search path, predefine variables in your workspace, or define defaults for Handle Graphics® objects. Use the following statements in a startup.m file to add the specified folder, D:\, to the search path, and to change the current folder to D:\ upon startup. Place the file in the default or current startup folder, which is where MATLAB first looks for it. You can place it in any other search path of MATLAB session.
addpath D:\
cd D:\
3. userpath
User environment path, on Windows, it is the user's "Documents" (or "My Documents" on WinXP) folder appended with "MATLAB".