Change date InputFormat across a cell
8 views (last 30 days)
Show older comments
Dear all,
How to change datetime format of this attached cell arrays from 1/1/1989 to 1989-1-1?
Here is my try, but it doesnt worked:
d = cellfun(@(OBS_MAX) datetime(OBS_MAX, 'InputFormat', 'yyyy-MM-dd'), OBS_MAX, 'UniformOutput', false);
Thank you all in advanced.
0 Comments
Accepted Answer
Voss
on 3 Nov 2022
load('OBS_MAX.mat')
OBS_MAX = cellfun(@set_dates_format,OBS_MAX,'UniformOutput',false);
function t = set_dates_format(t)
t.dates.Format = 'yyyy-MM-dd';
end
0 Comments
More Answers (1)
Walter Roberson
on 3 Nov 2022
You do not have a cell array of datetime objects: you have a cell array of tables, and you need to affect one particular variable inside the table. This is difficult to do without a helper function, but not difficult if you use a helper function.
d = cellfun(@bashdatesformat, OBS_MAX, 'uniform', 0)
function newT = bashdatesformat(T)
T.dates.Format = 'yyyy-MM-dd');
end
Without the helper function, it gets... messy... involving pulling apart tables and putting them back together into new tables. One might have hopes that you could at least do
cellfun(@(V) subsasgn(V, substruct('.', 'dates', '.', 'Format'), 'yyyy-MM-dd'), C, 'uniform', 0)
but that does not work in practice for this particular case. (It is a trick that can work for some other kinds of variables; see https://www.mathworks.com/matlabcentral/answers/440856-replacing-nans-with-zero-in-a-matrix-within-a-cell-array#answer_357449 )
0 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!