TA的每日心情 | 奋斗 2021-5-1 20:26 |
---|
签到天数: 2013 天 [LV.Master]伴坛终老
- 自我介绍
- 数学中国站长
群组: 数学建模培训课堂1 群组: 数学中国美赛辅助报名 群组: Matlab讨论组 群组: 2013认证赛A题讨论群组 群组: 2013认证赛C题讨论群组 |
发表于 2014-5-20 08:32
|显示全部楼层
|
|邮箱已经成功绑定
MATLAB中不同工具包.m文件重名的解决办法 频道:技术文章 发布时间:2012-6-19 阅读量:2968 1 ?5 \) R O. Z2 B i* m
一、问题描述
% j% s7 d2 p& S9 Y* k/ |MATLAB使用搜索路径来查找.m文件及其他相关文件。任何你想运行的文件必须保存在当前目录或保存在处于探索路径上的目录中。默认情况下,每次启动MATLAB时,都使用位于matlabroot\toolbox\local目录中的pathdef.m定义搜索路径。$ C: T: K7 S/ h- r1 {
在这多用户的情况下,有一个潜在的问题——不同的用户使用不同的工具包,这些工具包可能有同名的.m文件,那么只有处于搜索路径前面的.m文件会被执行,另一个.m文件,永远不会被执行。
3 N' j8 q' w/ H, x4 S( U7 V比如,我们的MATLAB中,同时安装了dynare(4.2.4)和Lesage空间计量模型(2010),这两个工具中均包含有hessian.m这个文件。而在MATLAB的搜索路径(search path)中,dynare排在Lesage空间计量模型前面,这样,默认情况下你就无法使用Lesage空间计量模型中的hessian.m。7 h) a2 A: t s% f' L# T8 r
二、解决方法' Y3 Z% N' P/ K& @( a" S
(一)在本机临时更改搜索路径; P1 N$ T w" p& k5 F) P
● 将你所需的同名的M文件放到搜索路径的最前面
6 C, R4 g9 i$ Y& G- t% h+ \path('<your .m path>',path)# j# {5 A# H0 }; O
如果原先<your .m path>已经存在于搜索路径中,则该目录会被移至搜索路径最前面。
) w( C* u0 r8 ^3 V. Y- p- G" q如果要在搜索路径中包含指定目录的子目录,可以先使用genpath得到所有子目录,再用path命令,如path(genpath('d:\yuan\documents\matlab'),path)。: y- s- X9 L& V) ]9 }! o" |
● 或者是删除造成同名的另一个工具包路径+ |# |8 q9 V7 ?/ m% r# U
if findstr('<造成同名的另一工具包路径>',path)+ M7 g. P- @: p
rmpath('<造成同名的另一工具包路径>')
- r9 H9 v( a: F. |% T/ Hend4 j5 X8 B8 z- [8 H+ v# O
以上更改并没有保存下来,MATLAB进程关闭后重启,将恢复默认搜索路径。
. F: @; P2 }& e- J9 n2 q$ p- G7 o1 f(二)在本机自定义搜索路径
8 Q; m/ E) k& O8 w) |' U) {% e& i1.通过pathdef.m
) \ Q( i1 H6 ]. r8 l9 I' `/ v; G如果pathdef.m在MATLAB的启动目录中(Startup Folder),则MATLAB通过启动目录中的pathdef.m定义搜索路径。因此当我们将matlabroot\toolbox\local中的pathdef.m拷贝到启动目录中,启动MATLAB进程,单击File-->Set path,根据需要设置并保存。% s: l. ]2 M% f$ v% T9 W
关于Startup Folder的信息,请参见“三、参考阅读”部分。
( J: T2 r# T" w) W2 r2.通过startup.m! `9 J R% ^" A/ d& p9 |1 o- {( g
可以通过startup.m指定启动选项,如可以在startup.m中加入修改路径的相关语句,然后将startup.m放到Startup Folder中,或放到搜索路径中的任何一个文件夹中。. T, S" T. u" D8 k e9 q# y
5 S: }$ a7 S5 @+ `3 R i
如果你需要在本机同时启动一个以上MATLAB进程,且每个进程都有不同的自定义搜索路径,则可以先建立多个启动MATLAB的快捷方式,然后修改其Startup Folder。0 x6 A& l9 T3 F# u
然后将pathdef.m放到每个进程指定的Startup Folder中。双击每个快捷方式,启动MATLAB后,再通过File-->Set path修改各自的搜索路径并保存。7 \( l0 X/ ?6 h. F5 m' { y2 _
或者将自定义的startup.m放到指定的Startup Folder中。
/ S# C, ?3 S$ B7 `/ w. i(三)在worker或lab上自定义搜索路径6 A8 `1 l8 R; `3 |2 @) N8 h q
当进行分布式或并行计算时,计算任务可能提交到其他计算节点,这时自定义搜索路径的方法至少有以下几种方法:3 S1 o6 o; x2 P# h D
1.通过jobStartup.m
+ ]+ j1 v/ H( i' u" T/ W. z# ~将matlabroot\toolbox\distcomp\user中的jobStartup.m拷贝到作业的PathDependencies属性所指定的目录中,然后使用修改路径的相关语句,如:
8 _1 N2 Z6 m2 j' f( Ipath('<your .m path>',path) 将你所需的同名的M文件放到搜索路径的最前面;
5 } y+ l, x9 |2 _+ i9 Prmpath('<造成同名的另一工具包路径>') 删除造成同名的另一个工具包路径。2 ?4 B9 |- z% s4 N3 ~7 l
注:在我们的HPC系统中,所有节点(登录节点和计算节点)的MATLAB默认搜索路径都是相同的。
( f$ J( p6 B$ @+ ~1 ]2.通过设定作业的PathDependencies属性
, ]; N0 O. s# _" t3 H2 ]( }" wPathDependencies所指定的目录会自动加入Worker搜索路径的最前面。
& g, ?8 Q& g. N) b/ E2 U作业的PathDependencies默认指向Z:\User\Documents\MATLAB\,且它是一个1*1的Cell Array。1 _' o8 i) a1 C* d
如何将其他目录加入到PathDependencie列表?; S! ~' T1 ^1 \! J
● 方法一:下面的语句将Z:\User\Documents\MATLAB中的Other目录添加到PathDependencie列表中2 v0 q5 G, ^/ F: @
j.pathdependencies=[j.pathdependencies;'Z:\User\Documents\MATLAB\Other']
+ }- h* y. s! t3 R之后,j.PathDependencie就是2*1的Cell Array了。% |* J; Z. b7 P) y' h
● 方法二:下面的语句将Z:\User\Documents\MATLAB及其子目录替代原有的PathDependencie列表
8 \- r2 t7 }7 K$ R6 n. X Y6 Qp=textscan(genpath('Z:\User\Documents\MATLAB'),'%s','delimiter',';');
$ H6 K! v' J) yj.pathdependencies= p{1,1};) f+ {+ r; x1 k$ }- v
3.在计算程序(.m)中直接修改搜索路径
5 n+ ], m1 Z. `$ ~0 }在每个task所指定的.m文件中,直接加入addpath或rmpath等相关命令修改搜索路径。
6 h& L5 s. c% a4 M* K三、参考阅读: K6 k+ z' p5 M) Z( o' }5 ^$ u
(一)Function Precedence Order
& A( T% {# m0 o: `+ i3 N1. Variable
O$ L: C$ t" m7 H+ q, }' t2. Nested Function/ W: p+ W h, t( i2 V! E+ U
3. Subfunction
5 {5 X3 u, w$ H: }( Q B4. Private function+ L) j9 Y' ~. |4 {2 Q2 I6 A7 e
5. Class constructor
; M( u$ V1 k8 X2 c$ `/ o1 J6. Overloaded method4 i) l" }$ K" G5 M1 `% c/ G
7. MATLAB function file in the Current Folder; H+ s. I3 F, I: X( x- z
8. MATLAB function file on the path, or MATLAB built-in function" K% w' ?9 j9 ~: v [7 u; }3 J" ]! r6 H
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).
9 Z4 y. w6 ~, Q7 Q& [2 E& R+ R, ?Multiple Implementation Types
# b0 r+ \9 F+ Q% u8 YThere 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
m/ g) p8 R/ x( O1. Built-in file4 g0 \" Y9 X2 c8 y( f3 ]! l
2. MEX-files8 W% e7 K4 G% b- L1 X3 ]1 X( L n
3. MDL (Simulink® model) file2 Y7 N0 [% Z; s, t8 ]
4. P-code file- M0 Y v, z, x: V" V9 _$ N
5. Program file with a .m file extension
4 f; I, g" ?! g8 g(二)current folder、startup folder、userpath
. H# i- W+ ~2 U, L8 c% N6 e1 D1. current folder
: V P% G9 m1 T+ @即当前目录! b; ?8 c7 W( K9 P9 E# W$ v
\4 T7 x% a: V* ?5 W2. startup folder. O% y- h1 W M3 J" d2 L
(1)什么是startup folder- V( r T3 P6 I% @. m8 o
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.
* @. ~( v# m" P d4 |+ t% uThe startup folder on Windows platforms depends on any startup options you specified and the way you started MATLAB:
* Q i8 l4 M( T$ a# P9 {7 g) D
9 Q2 r' x% |' `6 d8 [% B$ b" BHow Started
8 T& t" J n& S/ t L9 c7 P9 U | Startup Folder
% F0 N" Q) U( h1 V9 ~' M0 X& _7 ? | Double-click the MATLAB shortcut on your Windows desktop
! N+ A& S& M0 R; C2 x# |1 n& H | 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.
2 J- H! x8 G9 X/ [" c3 w" T1 I | Double-click a file type associated with MATLAB
5 k U1 ^5 T1 X. V | The folder in which the file resides is the startup folder. The userpath folder is automatically added to the search path.' f& }! S8 l& Y. l% [- z, a; W
| In a DOS window0 R2 K9 l# w- ^7 |
| The folder in which you ran the command is the startup folder. The userpath folder is automatically added to the search path.
. \. @8 |. C6 V |
4 H4 e0 |+ E+ P, \/ A5 F' C(2)更改startup folder
7 @7 d/ u# U9 C①通过userpath函数修改& }* L+ ]! l- R5 V; e
默认情况下,userpath即是startup folder,所以可以通过userpath来更改。一般不建议修改userpath。* N% @( g" {6 r2 d* U G, I/ N3 |
②在Windows平台下,通过快捷方式属性修改。
# k! k- @. L4 l2 o3 L右键单击快捷方式,选“属性”,打开如下对话框,将“起始位置(start in)”改为另外一个目录,如D:\HPCUserFile\yuan\Matlab。HPC二期的默认的起始位置是Z:\User\Documents\MATLAB。
, X9 h5 v* c' g9 w5 w/ i" @+ }0 Y. ^
③通过startup.m文件修改0 w8 C! A3 R6 T* l# Y9 W1 f/ E
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.
. {) w* d: \7 h. f: CThe 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.4 _" l( H' N# v
addpath D:\
, J% M* j7 z7 _6 X7 xcd D:\
' R: ]: L! N* G7 x1 x" E3. userpath
1 y# u, A, p& c; B) rUser environment path, on Windows, it is the user's "Documents" (or "My Documents" on WinXP) folder appended with "MATLAB".! K) E+ u. \) I& [$ k
7 h6 X3 S3 n/ w1 k% |6 K
4 a2 s) q1 C1 S |
|