findpeaks doesn't work with a different startup folder

1 view (last 30 days)
Hello I'm using Matlab 2017a on macOS 10.12.6. I wanted to use findpeaks function so I clean installed Matlab together with the Signal Processing Toolbox. Then I checked the examples for findpeaks function in the documentation and it works fine. But the moment I use startup.m file to start the Matlab session from another folder, the same example for findpeaks doesn't work and the use of command findpeaks(data) returns an empty matrix. Does anyone has any idea about what is this problem and how could this be fixed?
  3 Comments
Naveen
Naveen on 27 Jul 2017
I figured out the problem. It was because of the conflict with another function which has the same name as the build-in findpeaks. This file containing this function was somehow on the path and I deleted it and now it works fine.
Just out of curiosity, I have only used breakpoints for debugging the code. Is there any build-in debugging facility in Matlab that can clearly tell what is exactly the problem, like in this case?
Adam
Adam on 27 Jul 2017
Edited: Adam on 27 Jul 2017
You can use the Stop on errors option in the breakpoints menu to stop the code when an error occurs (i.e. equivalent to if you knew there would be an error and on what line and had added a breakpoint yourself), but not for something like this where it is just a case of you shadowing a builtin function (or not when your path changes).
When naming your own functions though you should get into the habit of calling
which myChosenFunctionName
on the command line to check if you are about to clash with a builtin function (where 'myChosenFunctionName' should obviously be replaced by whatever you are planning to name your function)

Sign in to comment.

Accepted Answer

Jan
Jan on 27 Jul 2017
Edited: Jan on 27 Jul 2017
Is there any build-in debugging facility in Matlab that can clearly tell
what is exactly the problem, like in this case?
No, this is not possible, even not in theory. Matlab has no chance to guess, that there is a problem at all: You have created a function called as a builtin function, but this might be intented. The function replies a valid output, and it would be very scary if Matlab tells you exactly, that you wanted to get something else and the reason for the difference.
Note that Matlab does display warnings, when built-in functions are shadowed. Create "plot.m" in a folder of Matlab's path to get:
Warning: Function plot has the same name as a MATLAB builtin.
We suggest you rename the function to avoid a potential name conflict.
(Who is "we"? The same as "they" in: "All I want to say is they don't really care about us"? Who is "us"??? Sorry, back to the topic:)
But finpeaks is not a built-in function, but provided as M-file. Then a shadowing is accepted.
I use FEX: UniqueFuncNames to check such problems automatically.
  2 Comments
Naveen
Naveen on 27 Jul 2017
I was expecting a warning like you showed in your comments. But somehow Matlab on my computer didn't display the warning. Maybe these warnings have to be enabled sometimes? Thanks for the suggestion about this other utility. I'll use it.
Jan
Jan on 27 Jul 2017
Edited: Jan on 27 Jul 2017
The warning appear for built-in functions only. These are functions of Matlab's toolboxes, which are not provided as M-files. findpeaks is not built-in. A warning about a shadowed function might be useful in your case, but there are many other cases, where the duplicate names are intented.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!