subsetting by date in matlab

To subset a time series by date in MATLAB, you can use the timerange function. timerange function selects a subset of a timetable, table, or schedule based on the time or duration. You can use timerange to subset the data from a timetable according to a date range.

Suppose you have a time series data in a timetabe tt and you want to subset it for the date range from '01-Jan-2018' to '31-Mar-2018', you can use the following code:

main.m
% create a timetable
date = {'01-Jan-2018','02-Jan-2018','03-Jan-2018','31-Mar-2018','01-Apr-2018','02-Apr-2018'};
data = [10.2,12.3,13.1,15.3,16.1,11.2];
tt = timetable(date', data',...
    'VariableNames',{'value'},...
    'RowTimes',datetime(date','InputFormat','dd-MMM-yyyy'));

% subset the timetable for the date range
subset_tt = timerange(tt, '01-Jan-2018', '31-Mar-2018');

% display the subset of timetable
disp(subset_tt)

434 chars
14 lines

This code uses the timerange function to subset the original timetable tt for the date range from '01-Jan-2018' to '31-Mar-2018' and stores the result in subset_tt. The resulting timetable subset_tt contains only the subset of data that falls within the specified date range.

Note that in the code above, we first create a timetable tt using the timetable function. We then specify the date range we want to subset by using a format that can be parsed by the datetime function. The timerange function is then used to extract the desired subset of the timetable.

I hope this helps!

related categories

gistlibby LogSnag