Vitrion
Vitrion

Reputation: 33

Function name conflict. How to call a MATLAB toolbox function instead an user-defined function

I have a problem that I could solve by changing my function's name. But I want to know if there's an option to call a MATLAB-defined function which has the same name of my user-defined function. By default, MATLAB always uses the user-defined function, but I want to use both in the same script. Any idea?

MATLABfuzzytoolbox::addrule(); userDefined::addrule()

Upvotes: 2

Views: 2461

Answers (3)

alexandre iolov
alexandre iolov

Reputation: 582

Would this be of help:

http://se.mathworks.com/help/matlab/ref/builtin.html

I am not sure if it works for 'classes' definitions only or if it works in general scripts/functions as well.

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112689

Get a handle to the original addrule function before you shadow it with your function:

fuzzy_addrule = @addrule;

The definition in this statement is "frozen", in the sense that if you later redefine or shadow addrule that won't affect fuzzy_addrule.

You can now define your addrule function, which will shadow the original addrule, but not fuzzy_rule.

addrule = @(x,y) x+y; %// very simple example

So, to use your function you simply write:

>> addrule(3,4)
ans =
     7 %// your function's result

To use the original function you call fuzzy_addrule:

>> fuzzy_addrule(readfis('tipper'),[]) %// example call for fuzzy/addrule function
ans = 
            name: 'tipper'
            type: 'mamdani'
       andMethod: 'min'
        orMethod: 'max'
    defuzzMethod: 'centroid'
       impMethod: 'min'
       aggMethod: 'max'
           input: [1x2 struct]
          output: [1x1 struct]
            rule: [1x3 struct]

The above requires that the handle to the toolbox function be created before you define your function. If you want to access the toolbox function after your function has been defined, you can do it as follows:

  1. Temporarily change to the toolbox folder, so that the toolbox function has precedence over functions on the path.
  2. Define the function handle fuzzy_addrule. Since the toolbox function is now visible, the handle refers to that function.
  3. Change back to the original folder. Now your function has precedence again (it shadows the toolbox function), but you have the handle fuzzy_addrule to the toolbox function.

Code:

curdir = pwd; %// take note of current folder
t = which('addrule', '-all'); %// t{1} is your function, t{2} is the toolbox function
fuzdir = regexp(t{2},'.+\\','match'); %// get only folder part
cd(fuzdir{1}); %// change to that folder
fuzzy_addrule = @addrule; %// define function handle
cd(curdir); %// restore folder

Once this has been done, each function can be called as described above.

Upvotes: 5

LowPolyCorgi
LowPolyCorgi

Reputation: 5169

Matlab uses the first function with the specified name that shows up in the path, and there is no mechanism to call specifically one of the functions sharing the same name.

A good practice is to put your functions into packages. In this way you can name them with the same name than a built-in function, but the call is slightly different: for instance if you have a function addrule in the package Pack (i.e. a file addrule.m in a folder +Pack), you can call it with Pack.addrule, while the built-in addrule function is simply called by addrule.

Best,

Upvotes: 7

Related Questions