简体   繁体   中英

Call overloaded function from specific toolbox in MATLAB

I have some Matlab-Toolboxes installed. In my Matlab version one of the Toolbox-Functions collides with another. In this case it is hessian. I want to use the hessian function of the symbolic-Toolbox.

When in C/C++ Functions are multiple defined like the function for cos and I still want to use the “standard” cos-function I can write:

std::cos(x);

Is there something similar in matlab?

If you have overloaded methods and want to call the built in one, you can use the function builtin . From the official documentation :

builtin(function,x1,...,xn) executes the built-in function with the input arguments x1 through xn. Use builtin to execute the original built-in from within a method that overloads the function. To work properly, you must never overload builtin.

The syntax for using it is:

[y1,...,yn] = builtin(function,x1,...,xn)

Friendly advice: If you want to try overloading builtin ("hmm, I wonder what would happen"), remember to save stuff first.

In a very similar way that you were describing for c/c++, you can use a specific toolbox function by adding the name of the toolbox first : ToolboxName\\function2call()

First use the which command to make sure of which function from which toolbox will be loaded with a specific call syntax.

Since I do not have the toolbox you are mentioning i'll use the classic fopen function as an example.

The first fopen function called without any other parameter will be the built in function which is used to return a handle to a file. Indeed, the which command confirms that:

>> which fopen
built-in (C:\TLAB13a\toolbox\matlab\iofun\fopen)

Now let's say I want to use the fopen function to open a serial port, I need to prefix the call to fopen with the name of the toolbox/object, like so: serial\\fopen . Let's first make sure this way of calling point to the right function:

>> which serial\fopen
C:\TLAB13a\toolbox\matlab\iofun\@serial\fopen.m  % serial method

Bingo !

And just to make sure this works when you are calling these functions, let's call them for real (with dummy parameters):

>> fopen('toto')
ans =
    -1

>> serial\fopen('toto')
Error using serial (line 72)
The PORT must be specified.

It worked. The first simple call to fopen('toto') return -1 because it couldn't find a file named "toto".
The second call to serial\\fopen('toto') errored because the serial port was not defined, but the right function was called.


Edit: You can also override the order in which Matlab will fetch functions by re-ordering the Matlab path. If you put the symbolic toolbox before the other one in your path, then when called without explicit information, Matlab will execute the first function it finds in the path.

I would still recommend the first solution with explicit declaration though, because changing the path order could mess up other function call in case you have many overloaded functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM