MATLAB Answers

Comparing date/time values in Matlab/ calling out date time values to use in code

35 views (last 30 days)
drb17135
drb17135 on 24 Feb 2021
Commented: Steven Lord on 25 Feb 2021
Hi,
So I have to sets of data one represents wind speed, and the other represents radar rain data.
My wind data is imported from excel and consists of a column of dates and the the remaining colums are values of wind speeds for different stations at these date times.
The radar data is a hdf file that has been converetd into rain intensity in another function.
Each set of data is in time increments of 5 minutes beginning September 2020.
E.g. first 4 sets of data :
  • '01-Sep-2020 00:00:00'
  • '01-Sep-2020 00:05:00'
  • '01-Sep-2020 00:10:00'
  • '01-Sep-2020 00:15:00'
I have looking to add a line that will compare the date/time values of the wind data to the date/time values of the radar data for each time point.
When the code finds a match, it can then be used in two other functinos I have.
A suggested start is
for i = 1:length(WindDatetime)
if RadarDatetime == WindDatetime(i)
then the file of interest == i
else
i =i +1;
end
end
However, this is not working for me.
I will insert the last part of my code that turns the date time vectors into a table of numbers like so :
2020 9 1 0 0 0
2020 9 1 0 5 0
2020 9 1 0 10 0
windtime = windfive.date; % extracting the date column from excel sheet
idx = ismember(windtime,timestampDT); % show which data is missing
windtime(~idx);
Winddata = datevec(windtime); %turn wind time data into a vector
Radardata = datevec(timestampDT); % turn radar time data into a vector
Radardata(any(isnan(Radardata),2),:) =[]; % delete any 'NaN' values
Another very useful thing to me would be if anyone knows a simple way of calling out time data so that I can use it.
For example if I would like to view the data between
'08-Sep-2020 23:55:00' and '17-Sep-2020 17:45:00'
is there a way of doing this?
Thanks:)

Accepted Answer

dpb
dpb on 24 Feb 2021
Convert to a timetable and all kinds of tools are available; see <Overview Timetables> for all of the above kinds of operations
  4 Comments
Steven Lord
Steven Lord on 25 Feb 2021
You don't turn a table or timetable into a timerange. You build a timerange from a pair of datetime or duration scalars. See the first example in the help text for timerange:
rng default % Make sure I get the same data every time I run this Answer
tt = array2timetable(randn(10,3),'RowTimes',datetime(2016,4,randi(15,10,1)))
tt = 10x3 timetable
Time Var1 Var2 Var3 ___________ ________ _________ ________ 11-Apr-2016 0.53767 -1.3499 0.6715 01-Apr-2016 1.8339 3.0349 -1.2075 05-Apr-2016 -2.2588 0.7254 0.71724 01-Apr-2016 0.86217 -0.063055 1.6302 02-Apr-2016 0.31877 0.71474 0.48889 13-Apr-2016 -1.3077 -0.20497 1.0347 11-Apr-2016 -0.43359 -0.12414 0.72689 05-Apr-2016 0.34262 1.4897 -0.30344 15-Apr-2016 3.5784 1.409 0.29387 01-Apr-2016 2.7694 1.4172 -0.78728
tr = timerange(datetime(2016,4,3),datetime(2016,4,12),'closed')
tr =
timetable timerange subscript: Select timetable rows with times in the closed interval: [03-Apr-2016 00:00:00, 12-Apr-2016 00:00:00] See Select Timetable Data by Row Time and Variable Type.
tt4to12 = tt(tr,:)
tt4to12 = 4x3 timetable
Time Var1 Var2 Var3 ___________ ________ ________ ________ 11-Apr-2016 0.53767 -1.3499 0.6715 05-Apr-2016 -2.2588 0.7254 0.71724 11-Apr-2016 -0.43359 -0.12414 0.72689 05-Apr-2016 0.34262 1.4897 -0.30344
In this code April 11th is between April 3rd and April 12th inclusive, so tt4to12 includes the first row of tt. April 1st however is not in that range, so tt4to12 does not include that row of tt.
If instead you wanted to ask "which rows of tt have Time in a specific subset" you'd want to use ismember.
rowsWith5Or11 = ismember(tt.Time, datetime(2016, 4, [5 11]))
rowsWith5Or11 = 10x1 logical array
1 0 1 0 0 0 1 1 0 0
tt5Or11 = tt(rowsWith5Or11, :)
tt5Or11 = 4x3 timetable
Time Var1 Var2 Var3 ___________ ________ ________ ________ 11-Apr-2016 0.53767 -1.3499 0.6715 05-Apr-2016 -2.2588 0.7254 0.71724 11-Apr-2016 -0.43359 -0.12414 0.72689 05-Apr-2016 0.34262 1.4897 -0.30344

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!