need to remove some time or make time

I have a datime matrice called tid with 8761 values such as tid = datetime(2015,1,1,00,00,00):hours(1):datetime(2016,1,1,00,00,00);
I'm trying to remove all the times exept the ones from 08-16 i wrote:
o=0;
for o = 9:24:8761-23
worktime(o,:) = tid([o:o+8],:)
if o>32 %remove for whole year after test
break %this to
end%this aswell
end
now it alomst devides the timetable in the manner I needed however i was expecting a row and there are some other problems becouse i don't know how to remove the Not a Times (NaT) now (pic below). I'm sure there's a better way to do this than what i'm thinking with a loop but I also have a seperat matrice with values that needs to be seperated in the same manner later so I thought test with the time first. and i don't think i can use cells in the script i have becouse of the newbcoding on my part.
I hope things are clear on what i need to do..

1 Comment

i think i could just remove them in the table menu however tidious that would be..

Sign in to comment.

 Accepted Answer

Use logical indexing. The relational operators like <= and > work on datetime arrays.
tid = datetime(2015,1,1,00,00,00):hours(1):datetime(2016,1,1,00,00,00);
August16thMidnight = datetime(2015, 8, 16);
August17thMidnight = August16thMidnight + days(1);
onAugust16th = August16thMidnight <= tid & tid < August17thMidnight;
tid(onAugust16th)

5 Comments

yes i think the computational time will be much shorter if i do it like you say however it would be tricky with 365 days
i was thinking an
%i moves in hours
%g moves in days
if ~ismember(tid(i,1), worktime(g,1))
codehere
elseif ismember (tid(i,1),worktime(g,1))
samecodehere(but differentvalues)
end
There are other functions that operate on datetime arrays that you could use to generate the logical array used to select a specific range. For instance if "worktime" means 9 AM to 5 PM on weekdays, the hour and isweekend functions can help you. (not a weekend, hour >= 9, hour < 17)
But since you mention "with 365 days" I'm wondering if you want to do grouped calculations:
  • process all data for January 1st
  • process all data for January 2nd
  • process all data for January 3rd
  • ...
  • process all data for December 31st
If so groupsummary and the other grouping and binning functions may be of use to you. Most if not all of them can accept and operate on datetime data.
Thank you! I didn't know this.
I would have thought an old-school guy like Steve would have suggested reshaping to 24x366 and deleting the first seven and last eight rows (you have to delete that last 2016 element first, though). But right, groupsummary etc. are worth looking at.
this is exactly what i did to get a quick fix but i'm gonna have to learn the grouping if i want to improve the code for general use purposes... but thank you

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!