English 中文(简体)
用MATLAB执行文件或调用文件放在另一个文件夹中的函数?
原标题:Executing a file or calling a function whose file is placed in another folder with MATLAB?
  • 时间:2011-05-27 14:02:03
  •  标签:
  • matlab

Tried Googling, but couldn t find anything.
I have a few files and folders in my current MATLAB folder.
One of those folders is called Map and it has a map1.m file which I want to call from my code in the current MATLAB folder.
In my code, I can t call it like this:

/Map/map1;

但我可以这样做:

cd Map;
map1;
cd ..;

不知怎么的,上面的方法似乎不正确。有更优雅的方法吗?

最佳回答

您可以使用运行命令,该命令专门用于此类情况。从文档中:

run是一个方便的函数,用于运行当前不在路径上的脚本。

您将函数/脚本调用为

run /Map/map1 

如果您只想通过输入函数/脚本的名称而不是完整(或相对)路径来运行函数/脚本,那么您应该将文件夹添加到您的路径中。

正如@mutzmatron所指出的,您不能使用run来调用带有输入/输出参数的函数。因此,除非它是一个没有输入/输出参数的脚本/函数,否则使用运行将不起作用,您必须将文件夹添加到路径中。


EDIT

正如一个良好的编码实践,以及在函数有输入/输出的情况下工作一样,从路径中添加/删除文件夹是正确的方法。因此,对于您的情况,

addpath /Map
...

map1;

...
rmpath /Map

重要的是,函数调用夹在addpathrmpath

问题回答

只需像gnovice建议的那样,使用addpath将所有这些目录添加到Matlab路径中。然后,您将能够正常调用这些函数,它们将对which()、help()、depfun()和其他Matlab元编程命令可见。您可以将addpath()调用放在startup.m文件中,使它们在每次启动Matlab时自动出现。

每次使用addpath/map1()/rmpath更改路径都有一些缺点。

  • It s a performance hit because you re adding path manipulation to each call.
  • Functions in different directories won t be able to see each other.
  • It ll be harder to write and debug functions because the path context in which they execute will change dynamically, and won t be the same as what you see when you re in the editor and the base workspace.
  • You need additional error handling code to make sure the path is properly restored if the called function errors out.
  • This won t work with the Matlab Compiler, if you want to deploy this code at some point.

而自己使用run()或cd()是很难看的,因为相对路径会有问题。

如果你真的想把子目录中的函数分开,这样它们就不能“看到”彼此,你可以在这些目录的名称前面加一个“+”,使它们成为名称空间,然后用名称空间限定函数调用,比如Map.map1()

只是为了促进改变道路的辩论。。。

让它更“安全”的一种方法是写

% start of my code: create function handles 
% to the functions I need:
try
   cd Map
   map1_func = @map1;
catch mexception
end
cd ..

这将尝试保留当前目录,并在另一个目录中获得该函数的句柄。

唯一的问题是,如果map1依赖Map目录中的其他函数,则方法将不起作用。





相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...