peaks of plot in UIAxes appdesigner opening in another window
4 views (last 30 days)
Show older comments
[sortedX, sortIndex] = sort(x);
sortedY = y(sortIndex);
sortedXLimit = sortedX(1:160,:);
sortedYLimit = sortedY(1:160,:);
plot(app.UIAxes,sortedXLimit,sortedYLimit)
app.UIAxes.YLim = [0 1000];
hold on;
findpeaks(sortedYLimit,sortedXLimit,'MinPeakProminence',205,'Annotate','extents')
my app design window has UI.axes and i have shown the plot of my data there, but the peaks are showing in matlab figure window, i do not understand the function on why matlab plots the peaks in a seperate window
0 Comments
Accepted Answer
Tommy
on 1 May 2020
Problem
According to the docs for findpeaks, a plot will be generated as long as no outputs are requested. After messing around with findpeaks for a bit, I cannot find a way to either (1) tell findpeaks which axes to use for this plot or (2) ask findpeaks which axes ended up being used. The code for findpeaks contains a line similar to the following line:
plot(x,y)
meaning that the plot will be added to whatever set of axes is returned by gca. It seems that gca can find an Axes object:
>> isequal(axes,gca)
ans =
logical
1
but not a UIAxes object:
>> isequal(uiaxes,gca)
ans =
logical
0
In the latter case, gca will instead create a new Axes object. Is that a bug or intentional?
Anyway, if you've plotted your data in regular old Axes, findpeaks will add to those axes, illustrated by the below code:
ax = axes;
x = 1:50;
y = rand(numel(x), 1);
plot(ax, x, y)
findpeaks(y, x, 'Annotate', 'extents')
But if you've plotted your data in UIAxes, findpeaks will instead create a new set of axes, illustrated by the below code:
ax = uiaxes;
x = 1:50;
y = rand(numel(x), 1);
plot(ax, x, y)
findpeaks(y, x, 'Annotate', 'extents')
Solution
I can think of two ways to get around this problem.
(1) Request output from findpeaks:
[pks,locs,w,p] = findpeaks(sortedYLimit,sortedXLimit,'MinPeakProminence',205);
and create the plot yourself. It will require some effort to create the plot, especially if you want the plot to look like the plot which findpeaks creates.
(2) Have findpeaks create the plot in a new set of axes, obtain the handle to those axes with gca, copy the plot elements over to your UIAxes, and then delete the newly created figure. Here is a simple example where I plot 50 random points, but the idea would be similar for your data:
ax = uiaxes;
x = 1:50;
y = rand(numel(x), 1);
plot(ax, x, y)
set(0,'DefaultFigureVisible','off') % hide the figure which findpeaks will create
findpeaks(y, x, 'Annotate', 'extents')
set(0,'DefaultFigureVisible','on') % reset default settings
ax2 = gca; % obtain newly created axes
children = findobj(ax2.Children, '-not', 'tag', 'Signal'); % get all graphics elements in those axes except for the signal, which we've already plotted
copyobj(children, ax) % copy the graphics objects to our UIAxes
legend(ax, 'String', ax2.Legend.String) % transfer the legend
grid(ax, 'on') % turn the grid on
delete(ax2.Parent) % delete the figure created by findpeaks
If anyone has suggestions or corrections for any of the above, or an easier way to solve this problem, please do tell!
2 Comments
Alon Zaharony
on 10 Mar 2022
The second solution works great for me! Thanks a lot!!
ax = uiaxes;
x = 1:50;
y = rand(numel(x), 1);
plot(ax, x, y)
set(0,'DefaultFigureVisible','off') % hide the figure which findpeaks will create
findpeaks(y, x, 'Annotate', 'extents')
set(0,'DefaultFigureVisible','on') % reset default settings
ax2 = gca; % obtain newly created axes
children = findobj(ax2.Children, '-not', 'tag', 'Signal'); % get all graphics elements in those axes except for the signal, which we've already plotted
copyobj(children, ax) % copy the graphics objects to our UIAxes
legend(ax, 'String', ax2.Legend.String) % transfer the legend
grid(ax, 'on') % turn the grid on
delete(ax2.Parent) % delete the figure created by findpeaks
More Answers (0)
See Also
Categories
Find more on Spectral Estimation 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!