Change date InputFormat across a cell

8 ビュー (過去 30 日間)
BN
BN 2022 年 11 月 3 日
回答済み: Walter Roberson 2022 年 11 月 3 日
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.

採用された回答

Voss
Voss 2022 年 11 月 3 日
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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 11 月 3 日
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 )

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by