peaks of plot in UIAxes appdesigner opening in another window
조회 수: 3 (최근 30일)
이전 댓글 표시
[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
채택된 답변
Tommy
2020년 5월 1일
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
Alon Zaharony
2022년 3월 10일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spectral Estimation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!