Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call a function that is not in the path in MATLAB?

I have installed a library that has some functions with the same name as MATLAB's. And by installing the lib, I mean addpath. When I try to call those function it'll use that lib's implementation but I want to call MATLAB implementation.

To make it simpler: how can I specify which function to call given that I have the absolute address of both functions?

I searched for the answer but I didn't find it on the website.

like image 410
Mohammad Moghimi Avatar asked Feb 09 '12 04:02

Mohammad Moghimi


People also ask

How do I run a function in another directory in MATLAB?

Use addpath() to add the other directory to the MATLAB path.

Can you call a function in MATLAB?

To call a function, we need to write in a line of code in the following order, output argument, followed by the “=” character, function name, and then the input arguments in parentheses.

Can you call a function within itself MATLAB?

Calling a function from within itself is called recursion and the simple answer is, yes.


2 Answers

If you overload any of the MATLAB built-in functions to handle a specific class, then MATLAB always calls the overloaded function on that type. If, for some reason, you need to call the built-in version, you can override the usual calling mechanism using the builtin function. The expression

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

forces a call to the MATLAB built-in function, reshape, passing the arguments shown even though an overload exists for the class in this argument list.

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

like image 183
Cheery Avatar answered Nov 01 '22 05:11

Cheery


use run, it will allow you to use your own functions instead of built-ins without adding them to the path.

Taken from help:

Run script that is not on current path Syntax

run scriptname

As @Cheery correctly said, it cannot be used for functions that accept arguments. However, run.m is modifiable file, so I made an extended version, that can accept arguments. It can be modified for output arguments as well quite easily.

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
like image 41
Andrey Rubshtein Avatar answered Nov 01 '22 05:11

Andrey Rubshtein