【问题标题】:Is it possible to call a function that is not in the path in MATLAB?是否可以调用不在 MATLAB 路径中的函数?
【发布时间】:2012-02-09 04:55:14
【问题描述】:

我安装了一个库,其中包含一些与 MATLAB 同名的函数。通过安装 lib,我的意思是 addpath。当我尝试调用这些函数时,它将使用该库的实现,但我想调用 MATLAB 实现。

为了更简单:如果我有两个函数的绝对地址,如何指定调用哪个函数?

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

【问题讨论】:

  • 图书馆有多大,你打算用它做多少工作?你想从你的代码中调用它的函数的频率是多少?库中有OO代码吗?

标签: function matlab


【解决方案1】:

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

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

强制调用 MATLAB 内置函数、reshape、传递显示的参数,即使此参数列表中的类存在重载。

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

【讨论】:

    【解决方案2】:

    使用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
    

    【讨论】:

    • 它是针对脚本的,而不是针对函数的!你试过了吗?您将无法以这种方式提供函数参数。函数文件调用的结果将类似于??? Input argument "x" is undefined.,其中“x”是函数的参数。
    • 当然。试试这个 - 函数文件function y = myfunc(x) (new line is here) y = x; disp(y); 将它保存为myfunc.m 某处并在Matlab 的命令行中尝试run path/myfunc.m。结果将是??? Input argument "x" is undefined. Matlab 有两种类型的文件 - 用于函数和脚本。您不能直接从编辑器或命令行运行函数文件。函数文件应该在路径中,Matlab 会在调用时尝试自行定位。
    【解决方案3】:

    当我将连续调用许多内置函数时,我喜欢的另一个解决方案是将我的库临时移动到路径的末尾。

    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)
    

    当然,如果您比库更频繁地使用内置函数,则可以将库保留在路径的末尾,并在使用时将其移动到顶部。请注意您的当前目录,但是,它总是会使用 precedence 而不是路径顺序。

    【讨论】:

      【解决方案4】:

      Andrey 的回答对我来说并不理想,但它和 Loren 的建议是“cd 到目录,创建 你的函数句柄,然后 cd back”让我想到了以下几点:

      定义一个执行 Loren 描述的函数:

      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 新用户注意:我建议谨慎使用这种方法!在某些情况下它可能非常有用,但总的来说,我会问自己是否没有更好的方法来处理您试图解决的问题。这可能会带来比解决问题更多的麻烦。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 2020-06-20
      • 2023-04-09
      • 2021-07-04
      相关资源
      最近更新 更多