I am having a problem with the findpeaks
function, this function is in the signal processing toolbox and also the program has another version of it (user defined function). I need to call the on in the signal processing toolbox not the user defined one, also I can't rename the user defined function for many reasons. Can anyone help me in calling the toolbox function.
-
just reorder the Matlab path, and make sure the specific toolbox where you want the function to be called is **before** the user defined function. When you're done, use the command `which findpeaks` to double check which one is going to be called. – Hoki Nov 18 '15 at 09:20
-
1Also: this is precisely why MATLAB recommends not using generic filenames for your user-defined functions. Call them e.g. `my_findpeaks` or something, to prevent these isues – Adriaan Nov 18 '15 at 09:24
-
1To resolve these ambiguities, _namespacing_ was created. I'd suggest to put the function into a Class, and then you would only be able to access it using `classNameOrInstance.findpeaks()`. – Dev-iL Nov 18 '15 at 09:46
-
@Hoki that's not a useful response as it requires manual intervention each and every time the desired function changes. In fact, this is why `R` introduced namespaces, so you can specify which version of a masked function you wish to call. – Carl Witthoft Nov 20 '15 at 16:36
-
@Adriaan that's what we in the business call a 'kludgey work-around' -- that's mathworks' fault, not yours. – Carl Witthoft Nov 20 '15 at 16:37
-
@CarlWitthoft. I know it's just a temporary workaround, that's why it's only a comment. There used to be a very convenient [namespacing solution](http://stackoverflow.com/questions/27016754/call-overloaded-function-from-specific-toolbox-in-matlab/27018169#27018169) but apparently it doesn't work anymore :-( Now you have to use Dev-iL comment if you want to do namespacing. – Hoki Nov 21 '15 at 13:37
-
Not quite a duplicate, but very closely related: https://stackoverflow.com/questions/11781634/how-to-wrap-an-already-existing-function-with-a-new-function-of-the-same-name – arr_sea May 12 '18 at 00:19
2 Answers
The precedence order used by MATLAB is described in their help pages. It states that functions in the current folder (9.) are preferred over functions elsewhere in the path (10.). Then, the first appearance of the function in the path is chosen. This allows for a number of possible solutions:
1. cd
to folder
A very simple method is simply to change the current workspace directory to the folder of the function you need to call, i.e. cd
either to the place where your user-defined function is, or cd
to the toolbox path. Note: This is rather inelegant, but probably sometimes the simplest solution.
2. Reorder path
As mentioned, MATLAB choses the first occurence of the function in the path. You can thus re-sort the path variable, so the folder where your user-defined function is, appears last. The path variable can be viewed and manipulated using the path
function. Note: Then you can only call the toolbox function. Otherwise you'd have to resort the path again.
3. Function handles
If you need to be able to call both functions, it can be useful to create a function handle for both versions. For that, you have to cd
into the folders where the functions are defined and create a new handle there:
cd('path/to/userdefined/function')
userFindPeaks = @findpeaks;
cd('path/to/MATLAB/installation/toolbox/signal/signal')
toolboxFindPeaks = @findpeaks;
You can then call the functions using feval
.
Of course, as Adriaan mentions in the comments, it is best not to use the names of already defined functions for your own functions or for variable names.

- 14,136
- 4
- 41
- 48
-
Shouldn't "4. Use signal.findpeaks directly or by handle" work? – Andras Deak -- Слава Україні Nov 18 '15 at 11:36
-
-
@hbaderts Sorry, I don't have `signal`, so I checked with `distributed.eye`. Turns out that it also a class, which is why it works... Sorry again! – Andras Deak -- Слава Україні Nov 18 '15 at 18:33
I just came here looking for the same thing... I ended up using builtin
.
https://uk.mathworks.com/help/matlab/ref/builtin.html
[y1,...,yn] = builtin(function,x1,...,xn)
@arr_sea actually posted a link in one of the folded comments which uses this function in a different context.

- 23
- 3
-
This solution is great, but only works for built-in functions. For the linked page: “A built-in function is part of the MATLAB executable. MATLAB does not implement these functions in the MATLAB language. [...] You can use the syntax `which function` to check whether a function is built-in.” The function in the OP, `findpeaks` is not built-in, it is an M-file (there are no toolbox functions that are built-in). – Cris Luengo Jun 12 '19 at 13:34