2012-02-09 46 views
12

我已经安装了一个库,它有一些与MATLAB相同的函数。通过安装lib,我的意思是addpath。当我尝试调用这些函数时,它会使用该库的实现,但我想调用MATLAB实现。是否可以调用不在MATLAB路径中的函数?

为了使它更简单:我怎样才能指定哪个函数来调用,因为我有这两个函数的绝对地址?

我搜索了答案,但我没有在网站上找到它。

+1

这个图书馆有多大,你会用它做多少工作?你多久想从代码中调用它的函数?库中是否有OO代码? – 2012-02-09 15:34:22

回答

10

如果您重载任何MATLAB内置函数来处理特定的类,那么MATLAB将始终调用该类型的重载函数。如果出于某种原因需要调用内置版本,则可以使用内置函数覆盖通常的调用机制。表达

builtin('reshape', arg1, arg2, ..., argN); 

强制MATLAB的内置函数的调用,重塑,通过即使存在这个参数列表类的重载所示的参数。

http://www.mathworks.com/help/techdoc/matlab_prog/br65lhj-1.html

8

使用run,它可以让你使用自己的功能,而不是内建的没有将其添加到路径。

从帮助中获取:

运行脚本不是当前路径上 语法

运行脚本名

正如@Cheery正确地说,它不能用于接受功能参数。但是,run.m是可修改的文件,所以我做了一个扩展版本,可以接受参数。它可以很容易地修改输出参数。

function runExtended(script,varargin) 

    cur = cd; 

    if isempty(script), return, end 
    if ispc, script(script=='/')='\'; end 
    [p,s,ext] = fileparts(script); 
    if ~isempty(p), 
     if exist(p,'dir'), 
      cd(p) 
      w = which(s); 
      if ~isempty(w), 
       % Check to make sure everything matches 
       [wp,ws,wext] = fileparts(w); 
       % Allow users to choose the .m file and run a .p 
       if strcmp(wext,'.p') && strcmp(ext,'.m'), 
        wext = '.m'; 
       end 

       if ispc 
        cont = ~strcmpi(wp,pwd) | ~strcmpi(ws,s) | ... 
         (~isempty(ext) & ~strcmpi(wext,ext)); 
       else 
        cont = ~isequal(wp,pwd) | ~isequal(ws,s) | ... 
         (~isempty(ext) & ~isequal(wext,ext)); 
       end 
       if cont 
        if exist([s ext],'file') 
         cd(cur) 
         rehash; 
         error('MATLAB:run:CannotExecute','Can''t run %s.',[s ext]); 
        else 
         cd(cur) 
         rehash; 
         error('MATLAB:run:FileNotFound','Can''t find %s.',[s ext]); 
        end 
       end 
       try 
        feval(s,varargin{:}); 
        %   evalin('caller', [s ';']); 
       catch e 
        cd(cur); 
        rethrow(e); 
       end 
      else 
       cd(cur) 
       rehash; 
       error('MATLAB:run:FileNotFound','%s not found.',script) 
      end 
      cd(cur) 
      rehash; 
     else 
      error('MATLAB:run:FileNotFound','%s not found.',script) 
     end 
    else 
     if exist(script,'file') 
      evalin('caller',[script ';']); 
     else 
      error('MATLAB:run:FileNotFound','%s not found.',script) 
     end 
    end 

end 
+1

它是为SCRIPTS,而不是功能!你尝试过吗?你将无法以这种方式提供函数参数。这个函数文件调用的结果就像这样'???输入参数“x”未定义。其中“x”是函数的参数。 – Cheery 2012-02-09 18:10:47

+0

当然。试试这个函数文件函数y = myfunc(x)(新行在这里) y = x; disp(y);'将其另存为'myfunc.m'并在Matlab的命令行中尝试'run path/myfunc.m'。结果将是'???输入参数“x”未定义。Matlab有两种类型的文件 - 用于函数和脚本。您不能直接从编辑器或命令行运行功能文件。函数文件应该在路径中,并且Matlab将尝试在调用时自己定位它。 – Cheery 2012-02-09 18:18:37

+0

@Cheery,完成更新:)再次感谢。 – 2012-02-09 18:22:58

2

另一个解决你的问题,我想,当我在一排被调用了大量的内置功能是我的图书馆暂时移动到路的尽头。

libpath = '/home/user/mylib'; 
% move mylib to the end of the path 
addpath(libpath, '-end'); 
% now call some built-in functions that mylib overwrites 
reshape(rand(100),10,10); 
% return mylib to the top 
addpath(libpath) 

当然,如果你使用内置的功能往往比你的libary的,你可以保持在库路径的终点,它只要您使用其移动到顶部。请注意您的当前目录,但是,在路径顺序上始终需要precedence

1

安德烈的回答是不理想的我,但它和罗兰的建议,“到该目录,创建 你的函数句柄,然后CD回来”让我想到以下几点:

定义一个函数,做什么罗伦描述:

function functionHandle = getFunctionHandleFromFile(fullFileName) 

[pathstr, name, ext] = fileparts(fullFileName); 

prevDir = pwd; 

cd(pathstr); 
functionHandle = str2func(name); 
cd(prevDir); 

然后,你可以用它来获得句柄。随着手柄,你可以调用函数:

nameOf = getFunctionHandleFromFile('/Users/sage/matlab-utilities/nameOf.m') 
nameOf(output) 

注到较新的MATLAB用户:我建议谨慎使用此方法!在某些情况下它可能非常有帮助,但总的来说,我会问自己,如果没有更好的方法来处理您正在尝试解决的问题。这可能会造成比解决更多的麻烦。

+0

问题是当函数想要从当前工作目录读取/写入文件时,您的方法会中断。 – 2013-09-09 17:33:26

+0

另请参阅相关问题http://stackoverflow.com/questions/13072470/call-a-function-that-is-not-on-the-matlab-path-without-adding-that-path/22532918#22532918和FEX贡献:http://www.mathworks.com/matlabcentral/fileexchange/45941-constructor-for-functionhandles – 2016-04-27 08:20:30

相关问题