Change date InputFormat across a cell

4 Ansichten (letzte 30 Tage)
BN
BN am 3 Nov. 2022
Beantwortet: Walter Roberson am 3 Nov. 2022
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.

Akzeptierte Antwort

Voss
Voss am 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

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 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 )

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by