Find coordinates of border in findpeaks plot

4 views (last 30 days)
Hi all,
I am struggling to find the cooridnates for the borders value while using the findpeaks function.
Here is a picture of my plot:
I would need to find the x coordinates of the borders between the peaks. I am doing so in order to get the duration of each peak delimited by the purble borders.
My code right now looks like this:
load('pO2,Mean,Numeric,Float,Raumedic,data_part1of1.mat');
ox = measurement_data;
smooth_ox = smooth(ox, 0.01, 'loess');
%find peaks
[pks,locs,wdths] = findpeaks(smooth_ox,'MinPeakProminence',0.5);
locmax = islocalmax(smooth_ox);
%plot
figure()
findpeaks(smooth_ox,'MinPeakProminence',0.5,'Annotate','extents',...
'WidthReference','halfheight')
text(locs+.02,pks,num2str((1:numel(pks))'))
title('Peaks')
Thank you for the help!

Accepted Answer

Star Strider
Star Strider on 5 Aug 2021
Using an example from the findpeaks documentation —
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 2 2 2 3];
Wdt = [3 8 4 3 4 6]/100;
Gauss = Hgt.'.*exp(-((x-Pos.')./Wdt.').^2);
smooth_ox = sum(Gauss);
%find peaks
[pks,locs,wdths] = findpeaks(smooth_ox,'MinPeakProminence',0.5);
locmax = islocalmax(smooth_ox);
%plot
figure
findpeaks(smooth_ox,'MinPeakProminence',0.5,'Annotate','extents',...
'WidthReference','halfheight')
text(locs+.02,pks,num2str((1:numel(pks))'))
title('Peaks')
Ax = gca; % Axes Handle
% GetAx = get(Ax) % Explore ...
% GetAx.Children % Explore ...
hlb = findobj(Ax,'Type','line', 'tag','Border') % Handles Array To Linew Sith 'Border' Tag
hlb =
2×1 Line array: Line (Border) Line (Border)
hlb(1).XData % Desired Result
ans = 1×12
146 146 NaN 419 419 NaN 587 587 NaN 1000 1000 NaN
hlb(2).XData % Desired Result
ans = 1×12
1 1 NaN 146 146 NaN 419 419 NaN 587 587 NaN
It might be necessary to use a for loop to get all of them in your situation (data not provided), however it should be straightforward to get them using this approach:
for k = 1:numel(hlb)
BorderX(k,:) = hlb(k).XData;
end
BorderX % View Result
BorderX = 2×12
146 146 NaN 419 419 NaN 587 587 NaN 1000 1000 NaN 1 1 NaN 146 146 NaN 419 419 NaN 587 587 NaN
Make appropriate changes to get the result you want.
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!