How to calculate the duration of data points in days

Hi,
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
Thanks!

 Réponse acceptée

Try something like this —
Data = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/772213/pulses.txt');
Days = 1:numel(Data);
NaNStart = strfind(~isnan(Data(:).'), [0 1])+1;
NaNStop = strfind(~isnan(Data(:).'), [1 0]);
DiffDays = NaNStop - NaNStart;
cntr = NaNStart + DiffDays/2;
figure
plot(Days, Data)
hold on
% plot(Days(NaNStart), Data(NaNStart), '>r')
% plot(Days(NaNStop), Data(NaNStop), '<r')
hold off
grid
text(cntr, ones(size(cntr)).*min(Data([NaNStart; NaNStop])), compose('\\leftarrow %.0f Days\\rightarrow', DiffDays), 'Horiz','center', 'Vert','middle')
.

5 commentaires

It works!
Thank you so much, a lot of appreciation.
but still I am leaning to understand what [0 1] and [1 0] does inline 3 and 4 respectively.
Thank you!
As always, my pleasure!
One relatively straightforward way of finding where a transition of [0 1] or [1 0] occurs in a vector is to use the strfind function, and here the [0 1] and [1 0] are the patterns to match. The strfind function returns the indices of the transitions beginning with the first match in each segment, so that has to be adjusted if the objective is to find the position of the 1 in the [0 1] transitions, although not in the [1 0] transitions. (It works because of the way vectors are represented in MATLAB, and was someone’s interesting discovery. I learned about it several years ago.)
.
Okay. I noted that and well understood. The index of change of transition from 0 to 1 or 1 to 0 makes a trick here. And the index date is used to know the start or end date. Amazing trick!!. Thanks a lot.
For my own reason I modified a little bit on the code, since my interest was to determine the duration, not the difference, hence I added 1 to the difference to include the start and end date and that works. Also MeanOf_HighPulses in the code below calculates mean duration in a year.
To avoid errors resulting from matrix dimention mis match, specially for data having only one pulse e.g as shown in the figure (top right corner) i used +1 in the difference:
DiffDays = (NaNStop - NaNStart)+1;
MeanOf_HighPulses = mean(DiffDays);
Best
Noted!
My code worked for the example shown, however it may not be uniformly robust to all such data (as you discovered). Your modifications to it are appropriate.
.
Yes, Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by