how to change the markers and line style, for 'findpeaks' ?

Hello, According to the next code, how can I change the ‘markers’ and line style using the ‘findpeaks’ function? Thank you.
[pks,locs,widths,proms] = findpeaks(pws(:,end),y,'MinPeakHeight',trd);
findpeaks(pws(:,end),y,'Annotate','extents','WidthReference','halfprom');
text(locs+0.5,pks,num2str((1:numel(pks))'));
legend('Filtered Data','Peak','Prominence','Width','FontSize',5);

 Réponse acceptée

Walter Roberson
Walter Roberson le 5 Mai 2021
Modifié(e) : Walter Roberson le 7 Oct 2021
ax = gca;
sig_h = findobj(ax, 'tag', 'Signal');
peak_h = findobj(ax, 'tag', 'Peak');
As you are asking to plot Extents as well, you will also have entries with tag 'Height', 'HalfHeightWidth', 'Border' (multiple); or 'Prominence', 'HalfProminenceWidth', 'Border' (multiple)
You can then set the LineStyle and Marker properties according to your preferences.

9 commentaires

@Walter Roberson Apparently it doesn’t work if you’re using a loop, here is the complete code. Thanks.
for cv6=1:N
ax6=subplot(4, 6, cv6, 'Parent', tab6);
sgtitle('Layering 30 minutes (2)');
[pks2{cv6},locs2{cv6},widths2{cv6},proms2{cv6}]= findpeaks((smt2{1,cv6}(:,end)),y,'MinPeakHeight',trd);
findpeaks((smt2{1,cv6}(:,end)),y,'Annotate','extents','WidthReference','halfprom');
text((locs2{cv6})+0.2,(pks2{cv6}),num2str((1:numel(pks2{cv6}))'));
legend('Filtered Data','Peak','Prominence','Width','FontSize',2);
ax6.YAxisLocation = 'right';
ax6.XDir = 'reverse';
ax6.XGrid = 'on';
ax6.YGrid = 'off';
name = sprintf('Layering (second) 30 minutes (%d)', cv6);
ylabel(name,'FontSize',5,'FontName','Arial','color','default');
xlabel('Altitude/km','FontSize',5,'color','default');
yticklabels('');
xlim(ax6,[75 95]);
ylim(ax6,[-inf inf]);
camroll(-90);
end
for cv6=1:N
ax6=subplot(4, 6, cv6, 'Parent', tab6);
sgtitle(tab6, 'Layering 30 minutes (2)');
[pks2{cv6},locs2{cv6},widths2{cv6},proms2{cv6}]= findpeaks((smt2{1,cv6}(:,end)),y,'MinPeakHeight',trd);
findpeaks((smt2{1,cv6}(:,end)),y,'Annotate','extents','WidthReference','halfprom');
text(ax6, (locs2{cv6})+0.2,(pks2{cv6}),num2str((1:numel(pks2{cv6}))'));
sig_h = find(ax6, 'tag', 'Signal');
peak_h = find(ax6, 'tag', 'Peak');
prom_h = find(ax6, 'tag', 'Prominence');
hp_h = find(ax6, 'tag', 'HalfProminenceWidth');
line_h = [sig_h, peak_h, prom_h, hp_h];
set(line_h, 'LineStyle', '--', 'Marker', '+');
legend(line_h, 'Filtered Data','Peak','Prominence','Width','FontSize',2);
ax6.YAxisLocation = 'right';
ax6.XDir = 'reverse';
ax6.XGrid = 'on';
ax6.YGrid = 'off';
name = sprintf('Layering (second) 30 minutes (%d)', cv6);
ylabel(name,'FontSize',5,'FontName','Arial','color','default');
xlabel('Altitude/km','FontSize',5,'color','default');
yticklabels('');
xlim(ax6,[75 95]);
ylim(ax6,[-inf inf]);
camroll(-90);
end
ok, I got the next message:
Error using find
Undefined function 'find' for input arguments of type 'matlab.graphics.axis.Axes'.
for cv6=1:N
ax6=subplot(4, 6, cv6, 'Parent', tab6);
sgtitle(tab6, 'Layering 30 minutes (2)');
[pks2{cv6},locs2{cv6},widths2{cv6},proms2{cv6}]= findpeaks((smt2{1,cv6}(:,end)),y,'MinPeakHeight',trd);
findpeaks((smt2{1,cv6}(:,end)),y,'Annotate','extents','WidthReference','halfprom');
text(ax6, (locs2{cv6})+0.2,(pks2{cv6}),num2str((1:numel(pks2{cv6}))'));
sig_h = findobj(ax6, 'tag', 'Signal');
peak_h = findobj(ax6, 'tag', 'Peak');
prom_h = findobj(ax6, 'tag', 'Prominence');
hp_h = findobj(ax6, 'tag', 'HalfProminenceWidth');
line_h = [sig_h, peak_h, prom_h, hp_h];
set(line_h, 'LineStyle', '--', 'Marker', '+');
legend(line_h, 'Filtered Data','Peak','Prominence','Width','FontSize',2);
ax6.YAxisLocation = 'right';
ax6.XDir = 'reverse';
ax6.XGrid = 'on';
ax6.YGrid = 'off';
name = sprintf('Layering (second) 30 minutes (%d)', cv6);
ylabel(name,'FontSize',5,'FontName','Arial','color','default');
xlabel('Altitude/km','FontSize',5,'color','default');
yticklabels('');
xlim(ax6,[75 95]);
ylim(ax6,[-inf inf]);
camroll(-90);
end
@Walter Roberson yes, that's the answer,, it worked so good, thanks a lot !!
@Walter Roberson I have tried to use the solution you proposed:
ax = gca;
sig_h = find(ax, 'tag', 'Signal');
peak_h = find(ax, 'tag', 'Peak');
Using it I am only able to change the style of peaks but I can not change the colour of the lines for prominence and halfprom
x = 1 : 50;
y = sin(x*2*pi/22);
findpeaks(y,x,'Annotate','extents','WidthReference','halfprom');
ax = gca;
sig_h = findobj(ax, 'tag', 'Signal');
peak_h = findobj(ax, 'tag', 'Peak');
prom_h = findobj(ax, 'tag', 'Prominence');
half_prom_h = findobj(ax, 'tag', 'HalfProminenceWidth');
prom_h.Color = [1 .3 1];
half_prom_h.Color = [.3 .8 .2];
For some reason the Prominence and HalfProminenceWidth tag is not recognized in my code
[pks_wc,locs_wc,widths,proms] = findpeaks(ampl_bgcorr,freq,'MinPeakDistance',p,'MinPeakProminence',10,'Annotate','extents','WidthReference','halfheight');
figure(1)
findpeaks(ampl_bgcorr,freq,'MinPeakDistance',p,'MinPeakProminence',10,'Annotate','extents','WidthReference','halfheight')
ax = gca;
sig_h = findobj(ax, 'tag', 'Signal');
peak_h = findobj(ax, 'tag', 'Peak');
proms_h = findobj(ax, 'tag', 'Prominence');
halfProm_h = findobj(ax, 'tag', 'HalfProminenceWidth');
peak_h.Marker = '.';
peak_h.MarkerSize = 10;
peak_h.Color = 'r';
sig_h.Color = 'b';
proms_h.Color = 'r';
halfProm_h.Color = 'r';
Change of the style of sig_h and peak_h are executed properly but for proms_h I get error message:
Property assignment is not allowed when the object is empty. Use subscripted assignment to create an array element.
Error in CWT_software_v4 (line 142)
proms_h.Color = 'r';
I think I will need your data to test with

Connectez-vous pour commenter.

Plus de réponses (1)

I always plot the results myself:
[peakValues, indexesOfPeaks] = findpeaks(................)
plot(x, y, 'b-', 'LineWidth', 2); % Plot original data.
hold on;
% Extract peak locations from x and y.
xp = x(indexesOfPeaks);
yp = y(indexesOfPeaks);
plot(xp, yp, 'rv', 'LineWidth', 2, 'MarkerSize', 10); % Plot triangles atop the peaks.
Or you could skip creating xp and yp and have for the last line
plot(x(indexesOfPeaks), peakValues, 'rv', 'LineWidth', 2, 'MarkerSize', 10); % Plot triangles atop the peaks.

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by