Using datetime function - questions on speed and specific time points

5 views (last 30 days)
Dear community,
I'm currently trying to run the code below to label some recorded data with specific times. However, when it is applied to a larger dataset it becomes incredible slow. Does anyone have any suggestions to make it faster?
In addition, I would like to be able to return a variable with 1s or 0s depending on whether the time interval was between 22:00:00 and 22:30:00 without considering the date, month, and year. Is that possible?
Best regards,
Eric
length_recording = [1000;3442;2321]; % Length of recordings, 1st 1000 seconds, 2nd 3442 seconds and 3rd 2321 seconds
d = ['2021-11-29 10:00:00';'2021-11-29 22:15:00';'2021-11-30 08:00:00']; % start times
for jj = 1:length(length_recording)
t = datetime(d(jj,1:length(d)),'InputFormat','yyyy-MM-dd HH:mm:ss');
for jj2 = 1:length_recording(jj,1)
if jj2 == 1
Actual_time(jj,jj2) = t; % Time point for the first
else
Actual_time(jj,jj2) = t+seconds(((jj2-1))); % The following numbers
end
end
end
for ll = 1:size(Actual_time,1)
if ll == 1
Actual_time_reshaped = Actual_time(1,:);
else
Actual_time_reshaped = [Actual_time_reshaped, Actual_time(ll,:)];
end
end
Actual_time_clean = Actual_time_reshaped(~isnat(Actual_time_reshaped))';
  2 Comments
dpb
dpb on 30 Nov 2021
It's unclear to me what you're really after here for absolute certainty, but the phrase " to label some recorded data with specific times." makes me think you want a timestable object.
Illustrate what you have to start with and what you want for a result for a small dataset it reading the doc for the timetable doesn't lead to nirvana.
dpb
dpb on 30 Nov 2021
The real Q? was answered below, but will make comment on the above to point out something important to know in MATLAB...in
length_recording = [1000;3442;2321]; % Length of recordings, 1st 1000 seconds, 2nd 3442 seconds and 3rd 2321 seconds
d = ['2021-11-29 10:00:00';'2021-11-29 22:15:00';'2021-11-30 08:00:00']; % start times
for jj = 1:length(length_recording)
...
the upper bound for jj in the for loop is undoubtedly going to be a surprise...
>> d = ['2021-11-29 10:00:00';'2021-11-29 22:15:00';'2021-11-30 08:00:00']; % start times
>> length(d)
ans =
19
>>
length() is a somewhat dangerous function in MATLAB, it is defined as max(size(x)) so for a character vector array as the above, what you get is the length of each character vector or size(d,2), not the expected/desired value of 3 or size(d,1)
This is also an opportunity to suggest to not use char() vectors in general but either cellstr() or, the more recently introduced strings array which as my code above illustrates can be created by using double quotes.
These have so many advantages over char string arrays for text handling there's essentially no use in ever using the other unless one is doing something clever like arithmetic on the ASCII values....there are places where such tricks are appropriated, but for general use, the char vector is passe and should be avoided.
Simliarly, in general avoid length to prevent such surprises as the above--use size with the specific dimension argument wanted or, for vectors as above, numel as I illustrate above.

Sign in to comment.

Answers (1)

dpb
dpb on 30 Nov 2021
Edited: dpb on 30 Nov 2021
OK, I'll take a chance after rereading the Q? again...
d0=datetime(["2021-11-29 10:00:00";"2021-11-29 22:15:00";"2021-11-30 08:00:00"], ...
'InputFormat','yyyy-MM-dd HH:mm:ss');
for i=1:numel(length_recording)
tVec{i}=d0(i)+seconds(0:length_recording(i)-1);
end
will produce a cell array of three datetime vectors at one second intervals based on the initial datetimes and the values in the length_recording array.
>> whos tVec
Name Size Bytes Class Attributes
tVec 1x3 54440 cell
>> tVec(1)
ans =
1×1 cell array
{1×1000 datetime}
>> tVec{1}([1 end])
ans =
1×2 datetime array
29-Nov-2021 10:00:00 29-Nov-2021 10:16:39
>>
As noted above, however, the timetable is probably the ticket here...
>> data_recording=randn(length_recording(1),1); % make up a sample dataset
>> ttT=timetable(data_recording,'SampleRate',1,'StartTime',d0(1)); % put it in a timetable
>> head(ttT)
ans =
8×1 timetable
Time data_recording
____________________ ______________
29-Nov-2021 10:00:00 1.50
29-Nov-2021 10:00:01 0.88
29-Nov-2021 10:00:02 0.15
29-Nov-2021 10:00:03 -0.29
29-Nov-2021 10:00:04 0.58
29-Nov-2021 10:00:05 -1.02
29-Nov-2021 10:00:06 -1.39
29-Nov-2021 10:00:07 -1.69
>> whos ttT
Name Size Bytes Class Attributes
ttT 1000x1 9033 timetable
>>
Either is very fast.
As for the latter Q?, sure...you'll need to create a (set of) variable(s) for grouping that are the times over which need to stratify.
If only by minute, then hours and minutes are sufficient. You'll need those to be a duration variable that can be compared.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!