是否可以调用MATLAB中不在路径中的函数?

Moh*_*imi 12 matlab function

我安装了一个库,它有一些与MATLAB同名的函数.通过安装lib,我的意思是addpath.当我尝试调用那些函数时,它将使用lib的实现,但我想调用MATLAB实现.

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

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

Che*_*ery 10

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

builtin('reshape', arg1, arg2, ..., argN);
Run Code Online (Sandbox Code Playgroud)

强制调用MATLAB内置函数,重新整形,传递显示的参数,即使此参数列表中的类存在重载.

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


And*_*ein 8

使用run,它将允许您使用自己的函数而不是内置函数,而无需将它们添加到路径中.

取自帮助:

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

运行scriptname

正如@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
Run Code Online (Sandbox Code Playgroud)