- 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?
22 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Pavan Sahith
2024-7-30
编辑:Pavan Sahith
2024-7-31
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 个评论
Steven Lord
2024-7-30
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
2024-7-30
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);
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!