Comparing date/time values in Matlab/ calling out date time values to use in code
1 次查看(过去 30 天)
显示 更早的评论
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:)
0 个评论
采纳的回答
dpb
2021-2-24
Convert to a timetable and all kinds of tools are available; see <Overview Timetables> for all of the above kinds of operations
4 个评论
Steven Lord
2021-2-25
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)))
tr = timerange(datetime(2016,4,3),datetime(2016,4,12),'closed')
tt4to12 = tt(tr,:)
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]))
tt5Or11 = tt(rowsWith5Or11, :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!