Convert the format of the time column in a timetable
12 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!