How to calculate the duration of data points in days

2 views (last 30 days)
I have a daily data series with pulses. The data is for 1 year (starts in 1st day and end 365 days) but there are NAN values between as shown in image below.
Then I want to determine individual duration and mean duration for the three pulses with data e.g the duration from a1-a2, b1-b2, and c1-c2. Since i do have alot of data of this type i wanted some kind of loop to do the problem, because manual counting takes to much time.
Can someone help me with some code to determine this? Alternatively, a different way of determining the same thing would be appreciated.
Start_Date = datenum(1995,01,01);
End_Date = datenum(1995,12,31);
YearlyVEC = datevec((Start_Date:End_Date)');
So i wanted to determine the day also from the variable YearlyVEC,
a data of the pulses is also attched below

Accepted Answer

Star Strider
Star Strider on 19 Oct 2021
Try something like this —
Data = readmatrix('');
Days = 1:numel(Data);
NaNStart = strfind(~isnan(Data(:).'), [0 1])+1;
NaNStop = strfind(~isnan(Data(:).'), [1 0]);
DiffDays = NaNStop - NaNStart;
cntr = NaNStart + DiffDays/2;
plot(Days, Data)
hold on
% plot(Days(NaNStart), Data(NaNStart), '>r')
% plot(Days(NaNStop), Data(NaNStop), '<r')
hold off
text(cntr, ones(size(cntr)).*min(Data([NaNStart; NaNStop])), compose('\\leftarrow %.0f Days\\rightarrow', DiffDays), 'Horiz','center', 'Vert','middle')

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by