- readtimetable: https://www.mathworks.com/help/matlab/ref/readtimetable.html
- timerange: https://www.mathworks.com/help/matlab/ref/timerange.html
how to extract range of dates from timetable?
8 views (last 30 days)
Show older comments
hi all,
The attached screenshot shows some data I need to extract based on time from the timetable.
For example, all measurements were collected on July 8, 2021, then all measurements were collected on July 9, 2021, and so on.
your help is appreciated
0 Comments
Accepted Answer
Pavan Sahith
on 30 Jul 2024
Edited: Pavan Sahith
on 31 Jul 2024
Hello Lilya,
I assume your data is in an Excel sheet, you can read it into MATLAB using the 'readtimetable' function.
TT = readtimetable('data.xlsx');
To extract data for a specific date, such as July 8, 2021, you can use the timeRange function. Refer to this sample code:
% Define the date range
startDate = datetime(2021, 7, 8);
endDate = datetime(2021, 7, 8, 23, 59, 59);
% Create a timerange
timeRange = timerange(startDate, endDate);
% Extract data within the specified date range
TT_July8 = TT(timeRange, :);
To extract data for multiple dates, you can use a loop or array of date ranges:
% Define the date ranges
dateRanges = [datetime(2021, 7, 8); datetime(2021, 7, 9); datetime(2021, 7, 10)];
% Initialize a cell array to store the results
extractedData = cell(length(dateRanges), 1);
% Loop over each date and extract data
for i = 1:length(dateRanges)
startDate = dateRanges(i);
endDate = startDate + hours(23) + minutes(59) + seconds(59);
timeRange = timerange(startDate, endDate);
extractedData{i} = TT(timeRange, :);
end
% Access extracted data for specific dates
TT_July8 = extractedData{1};
TT_July9 = extractedData{2};
TT_July10 = extractedData{3};
By following these steps, you can extract measurements collected on specific dates from your timetable.
Consider referring to the following Mathworks Documentation to know more
Hope this helps you in moving forward
2 Comments
Steven Lord
on 30 Jul 2024
Depending on what @Lilya is trying to do with the daily data, the retime function for timetable arrays or the groupsummary function for table, timetable, or numeric arrays may be of use.
Walter Roberson
on 30 Jul 2024
startDate = datetime(2021, 7, 8);
endDate = datetime(2021, 7, 8, 23, 59, 59);
% Create a timerange
timeRange = timerange(startDate, endDate);
The default IntervalType for timerange() is openright -- that is, by default the exact start time is included and the exact end time is excluded. So the appropriate coding would be closer to
startDate = datetime(2021, 7, 8);
endDate = startDate + 1;
% Create a timerange
timeRange = timerange(startDate, endDate);
or for greater certainty
startDate = datetime(2021, 7, 8);
endDate = dateshift(startDate + 1, 'start', 'day');
% Create a timerange
timeRange = timerange(startDate, endDate);
More Answers (1)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!