Using findpeaks from within a callback function

3 Ansichten (letzte 30 Tage)
Martin Thompson
Martin Thompson am 4 Mär. 2018
Kommentiert: Image Analyst am 4 Mär. 2018
I'm using GUIDE, and have a timer that initiates a callback function every second. This function is used to plot data. I would like to use the findpeaks function within the callback, but there is a problem with the scope, as it will either do nothing or open a new figure window to show the peaks.
I can use the findpeaks function and store the [peaks locs] and plot them that way using update ydata and so on, but then I miss out on all the other neat features like widths and heights etc.
Has anyone had any experience using the findpeaks function within a callback?

Antworten (1)

Image Analyst
Image Analyst am 4 Mär. 2018
If you use output arguments, findpeaks() will not do any plotting or open a new figure.
If you store peaks and locs, I don't see why this would preclude you from sending in input arguments to specify parameters about the widths and heights of the peaks.
I don't know what "update ydata" means without see the code for it. I know of the plot() function but not the update() function. Exactly what does "plot them that way using update ydata" mean??? What is ydata? Is it the same as the "data" array you referred to earlier in your question?
I've used findpeaks() in a callback function before and have not found any difference in how it operates between a function and a script or command line.
  2 Kommentare
Martin Thompson
Martin Thompson am 4 Mär. 2018
Hi Image analyst, this is my current code, using findpeaks on its own instead of working around this way does not work. Even if I set the current axes using axes() or set(), it still opens a new figure instead of plotting on the specified axis.
function update_display(hObject,eventdata,hfigure)
% Timer timer1 callback, called each time timer iterates.
% Update plot data
handles = guidata(hfigure);
[~,handles.locs] = findpeaks(handles.ppg)
set(handles.main_plot,'YData',handles.ppg)
set(handles.peaks_plot,'YData',handles.ppg(handles.locs),'XData', handles.locs)
hold(handles.main_axes,'on')
drawnow;
guidata(hfigure, handles);
% END USER CODE
Image Analyst
Image Analyst am 4 Mär. 2018
Why did you change the callback to take hfigure instead of handles? Don't do that. Just take handles in as normal, then refer to your figure using its known tag. Also, don't call guidata() at all unless you changed handles inside the callback function, and even then, if you do need to call it, call it at the very end of the function, not at the beginning. I suspect changing the normal way of handling things to your more convoluted way could be causing the problem.
And, assuming you're not using some antique version of MATLAB, don't use the old set way. Use the new OOP way, which is like the way most other languages do it:
Old way:
set(handles.main_plot,'YData',handles.ppg)
New way:
handles.main_plot.YData = handles.ppg;
That wouldn't cause the error, I don't believe, but it would convert your code into the modern style we use these days.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by