how to extract range of dates from timetable?

13 Ansichten (letzte 30 Tage)
Lilya
Lilya am 30 Jul. 2024
Beantwortet: Lilya am 31 Jul. 2024
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

Akzeptierte Antwort

Pavan Sahith
Pavan Sahith am 30 Jul. 2024
Bearbeitet: Pavan Sahith am 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 Kommentare
Steven Lord
Steven Lord am 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
Walter Roberson am 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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Lilya
Lilya am 31 Jul. 2024
those are very very very helpful solutions!!
thank you everyone

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by