Convert the format of the time column in a timetable

11 ビュー (過去 30 日間)
Pietro
Pietro 2023 年 5 月 12 日
回答済み: Shaik 2023 年 5 月 12 日
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

回答 (1 件)

Shaik
Shaik 2023 年 5 月 12 日
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;

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by