Info

This question is closed. Reopen it to edit or answer.

Search for timedates in a table

1 view (last 30 days)
Tiago Dias
Tiago Dias on 7 Feb 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello, I got a table A 10 rows and 1 column with dates '01/01/2018 O1:00:00' '01/01/2018 02:00:00' ... '01/01/2018 10:00:00'
and for example, I just want to extract from here the values from 01/01/2018 02:00:00 to 01/01/2018 05:00:00, how can i do that?
clear;
clc;
close all;
A = readtable('1.xlsx');
t_start = datetime('01/01/2018 02:00:00');
t_end = datetime('01/01/2018 05:00:00');
for i = t_start:t_end
B = A(i,1);
end
i run this but nothing happens

Answers (1)

Steven Lord
Steven Lord on 7 Feb 2018
From the documentation: "Create a sequence of datetime values starting from November 1, 2013 and ending on November 5, 2013. The default step size is one calendar day."
When you call the colon operator with a datetime as the start and end but no increment, MATLAB will step in increments of 1 day. If you want to step in increments of 1 hour, you will need to specify an increment.
y = t_start:hours(1):t_end
But that probably won't help with the way you're trying to index into your table. If you stored your data in a timetable array instead, you could use timerange and withtol to index into the rows of your timetable for a specific time or range of times.
  1 Comment
Peter Perkins
Peter Perkins on 7 Feb 2018
As Steve says, if you have R2016b or later, this is built into timetable subscripting. In earlier releases, you can use between on the time vector to create a logical vector that will pick out rows of your table.

Community Treasure Hunt

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

Start Hunting!