how to extract range of dates from timetable?

조회 수: 13 (최근 30일)
Lilya
Lilya 2024년 7월 30일
답변: Lilya 2024년 7월 31일
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

채택된 답변

Pavan Sahith
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
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
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개)

Lilya
Lilya 2024년 7월 31일
those are very very very helpful solutions!!
thank you everyone

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by