Convert the format of the time column in a timetable

11 Ansichten (letzte 30 Tage)
Pietro
Pietro am 12 Mai 2023
Beantwortet: Shaik am 12 Mai 2023
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

Antworten (1)

Shaik
Shaik am 12 Mai 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;

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by