How do I call function with the same name from different toolbox?
Show older comments
I am trying to use the "tf" function that takes a digitalFilter object as the input and outputs the numerator and denominator vectors. However the control system toolbox tf function does the opposite.
Answers (2)
Star Strider
on 11 Jun 2020
lpFilt = designfilt('lowpassiir','FilterOrder',8, ...
'PassbandFrequency',35e3,'PassbandRipple',0.2, ...
'SampleRate',200e3); % Example From ‘designfilt’ Documentation
[num,den] = tf(lpFilt); % Transfer Function Polynomials
.
Image Analyst
on 11 Jun 2020
If you do
>> which -all functionName
and it shows several functions in different folders then MATLAB will take the top/first one. To see what functions you have that are overloaded, run my attached utility, what_is_overloaded.m.
If you want to run one that is second or lower on the list, you need to temporarily remove the path of the first one, then call your function, then restore the path of the first one.
rmpath(folder1); % Remove path of first functionName from search path.
% Now it will no longer see the first functionName, unless it's in the current folder.
functionName();
addpath(folder1); % Restore original folder.
I think you could also do it by changing directory to the folder of the one you want to run temporarily:
currentFolder = pwd; % Save original folder
cd(folderWhereFunctionNameLives); % Change to the folder of the functionName that you WANT to run lives.
% Now it will no longer see the first functionName, unless it's in the current folder.
functionName();
cd(currentFolder); % Restore original folder.
8 Comments
Steven Lord
on 11 Jun 2020
Your statement is technically correct, though slightly misleading in a way that matters for this question.
The output of which -all will show you functions and methods by that name. When you try to call a function (or method) by that name, MATLAB will follow the function precedence order. Both object functions (methods) and class constructors, among others, fall earlier in the function precedence order than functions on the path. But if you're calling a function not a method you're correct that the top one in the list "wins".
Let's take a simple example: the mean function. When I run which -all mean in my installation of release R2020a with only a few toolboxes:
>> which -all mean
C:\Program Files\MATLAB\R2020a\toolbox\matlab\datafun\mean.m
C:\Program Files\MATLAB\R2020a\toolbox\matlab\datatypes\duration\@duration\mean.m % duration method
C:\Program Files\MATLAB\R2020a\toolbox\matlab\datatypes\datetime\@datetime\mean.m % datetime method
C:\Program Files\MATLAB\R2020a\toolbox\matlab\bigdata\@tall\mean.m % tall method
C:\Program Files\MATLAB\R2020a\toolbox\parallel\parallel\@codistributed\mean.m % codistributed method
C:\Program Files\MATLAB\R2020a\toolbox\parallel\gpu\@gpuArray\mean.m % gpuArray method
C:\Program Files\MATLAB\R2020a\toolbox\matlab\timeseries\@timeseries\mean.m % timeseries method
If I call mean on a duration or datetime object, the mean.m in toolbox\matlab\datatypes\duration\@duration or toolbox\matlab\datatypes\datetime\@datetime is called instead of the mean.m in toolbox\matlab\datafun despite being lower on the list than the file in datafun. If you want to check which function will be called, you can call which with the syntax given in the "Locate Function Invoked with Given Input Arguments" example on its documentation page. [Things get a bit more complicated if your function/method call has multiple objects of different classes.]
>> T = datetime('today');
>> which mean(T)
So getting back to the original question, if you want to invoke the tf method of the digitalFilter class, call it with a digitalFilter object as input as Star Strider showed.
Image Analyst
on 11 Jun 2020
So if it would throw an error because you didn't pass it the right arguments (arguments it does not understand or is not capable of dealing with), it basically goes down the list until it finds the function that seems to like those type of arguments and then use that function? And wouldn't throw an error unless and until it has checked out all the possible functions and found that none of them would work?
Steven Lord
on 11 Jun 2020
Work down the function precedence order. If I called mean with a sym object from Symbolic Math Toolbox (which I have installed, meaning from the output of which I posted earlier it does not overload mean as a method) let's check what gets called.
1) Is there a variable named mean in the workspace? There wasn't when I called which to generate the output above.
2) Did I import a package function named mean? Executing import mypackage.mean would let me call mypackage.mean just as mean(...). I didn't; I haven't written any such package function.
3) Is there a nested function named mean in the current function? Let's say I'm going to call mean from the MATLAB prompt so no.
4) Is there a local function in the curent file? Nope.
5) Did I import using a wildcard, like import mypackage.*? No.
6) Is there a private function named mean? No.
7) Is there a method of sym named mean? From the output of which above we can see that there isn't.
8) Is there a class named mean? If there were we'd try to instantiate an instance of that class, but there isn't.
9) Is there a loaded Simulink model named mean? No.
10) Is there a function named mean in the current folder? You can't see what my current folder is, but there isn't.
11) Is there a function named mean on the path (in order of appearance)? Yes! So MATLAB will try to run the mean.m in toolbox\matlab\datafun with my sym object as input. Let's confirm that.
>> x = sym(1:10);
>> dbstop in toolbox\matlab\datafun\mean.m
>> mean(x)
48 isDimSet = nargin > 1 && ((~ischar(dim) && ~(isstring(dim) && isscalar(dim))) || ...
That is in fact the first line of toolbox\matlab\datafun\mean.m in release R2020a.
If instead I'd called mean on a duration array I would have answered Yes! and stopped at step 7 rather than going all the way through step 11. Step 7 can get trickier if multiple objects are involved; then class precedence also needs to be taken into account.
If I'd tried to call a function that didn't exist, answering No at step 11 means MATLAB would error.
>> thisFunctionDoesNotExist(1:10)
Unrecognized function or variable 'thisFunctionDoesNotExist'.
Typing all this out took a little while, but luckily MATLAB doesn't need to type it out. It just decides and does the right thing.
Shrikumar Patil
on 8 Dec 2020
How can I add another path, so that it shows up in 'which -all functionName'? Please..
I have another same function in another directory, but it doesn't show up when I do 'which -all functionName'.
Also, when I try to change to the directory of that function, it still fails to get that one, and goes back to calling the unwanted one. (or default one)
Image Analyst
on 8 Dec 2020
Don't change directory. See the FAQ:
Just add the path and save it. I believe it will go to "the front of the line" so to speak and your function in that folder will be chosen first.
addpath('C:/whatever/fubar/snafu/my m-files'); % Add your folder to the path.
savepath(); % Save it permananently, for future sessions of MATLAB.
If you have subfolders in there you'd also like to add, use genpath():
addpath(genpath('C:/whatever/fubar/snafu/my m-files'));
savepath();
If you don't call savepath(), it will still be on the path but just for the current session and won't be there if you shutdown MATLAB and restart it.
Steven Lord
on 9 Dec 2020
As stated in the documentation the addpath function will add directories to the top of the path when called with the -begin option (or without a position option.) If you specify the -end option in the call it will add the directories to the bottom of the path instead.
Walter Roberson
on 9 Dec 2020
I also recommend using pathtool -- it gives a nice visual interface.
I have another same function in another directory, but it doesn't show up when I do 'which -all functionName'.
Functions in the current diretory always have priority over functions in any other directory when you call them by name. Therefore if you have a function (not a method) by the same name in your current directory, and in another directory, and you want to call the one in the other directory, you cannot do that directly.
What you can do is
curdir = cd('ThatOtherDirectory')
SomeVariableName = @NameOfFunction
cd(curdir);
after which
SomeVariableName(parameter, parameter, parameter...)
will invoke the one from that other directory, not the one from the current directory. Function handles remember the path to the file and use it when the function is called.
Fabrice Lambert
on 2 Oct 2021
Is there any way to toggle the various functions in the help? For example, in my installation when I press F1 on "predict" it opens the Neural Network function. But what if I want to see the help for the linear model predict? Is there a shorter way than having to scroll through all the options in the documentation help?
Categories
Find more on Desktop in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!