Convert the format of the time column in a timetable

11 views (last 30 days)
I am dealing with monthly timeseries points displayed in timetables. However, they all contain the time vector in the dd-mm-yyyy format. Is there a way to convert them in Month-YYYY format disregarding the day?
Thank you in advance

Answers (1)

Shaik
Shaik on 12 May 2023
Hi Pietro,
Hope you are doing well, here is a code which can help you to check:
% Create a sample timetable with daily time points
startDate = datenum('01-Jan-2021'); % Specify the start date
endDate = datenum('31-Dec-2021'); % Specify the end date
timeVector = startDate:endDate;
data = randn(length(timeVector), 1); % Generate random data
t1 = timetable(datetime(timeVector', 'ConvertFrom', 'datenum'), data);
% Convert timetable from daily to monthly time points
t1_monthly = retime(t1(:, 'data'), 'monthly', 'mean');
% Extract the month and year from the time vector and create a new datetime
% array with the format 'mm-yyyy'
monthYearVector = datetime([t1_monthly.Time.Year, t1_monthly.Time.Month, ones(height(t1_monthly), 1)], 'Format', 'MM-yyyy');
% Convert the monthly datetime array to a cell array of strings with the
% format 'Month-YYYY'
monthYearStrings = cellstr(datestr(monthYearVector, 'mmmm-yyyy'));
% Assign the cell array of strings to a new column in the original timetable
t1_monthly.MonthYear = monthYearStrings;

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!