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:
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')
findpeaks(y, x, 'Annotate', 'extents')
set(0,'DefaultFigureVisible','on')
ax2 = gca;
children = findobj(ax2.Children, '-not', 'tag', 'Signal');
copyobj(children, ax)
legend(ax, 'String', ax2.Legend.String)
grid(ax, 'on')
delete(ax2.Parent)
If anyone has suggestions or corrections for any of the above, or an easier way to solve this problem, please do tell!