Change date InputFormat across a cell
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
댓글 수: 0
추가 답변 (1개)
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 )
댓글 수: 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!