フィルターのクリア

how to extract range of dates from timetable?

7 ビュー (過去 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 Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by