Borrar filtros
Borrar filtros

Convert the format of the time column in a timetable

8 visualizaciones (últimos 30 días)
Pietro
Pietro el 12 de Mayo de 2023
Respondida: Shaik el 12 de Mayo de 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

Respuestas (1)

Shaik
Shaik el 12 de Mayo de 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;

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by