how to synchronize structures with date fields in text format

I have a 31x1 cell array C. Each cell of C contains a structure of variable dimensions (41x1 to 50x1) but all structures have the same number of fields. Each structure has a ‘date’ field. The dates in each ‘date’ field are in text format.
I want to transform each structure in C into a timetable so I can synchronize them. I tried struct2table and then table2timetable but it does not work because the dates are not in datetime format. How do I change the format of the dates in the ‘date” field of each structure, so I can convert the structures in timetables and then synchronize them?
Thank you

 採用された回答

Walter Roberson
Walter Roberson 2020 年 3 月 28 日

0 投票

dateinfo = cellfun(@(S) datetime({S.date}), C, 'uniform', 0);
TT = cellfun(@(S, T) table2timetable(struct2table(S), 'RowTimes', T), C, dateinfo);

4 件のコメント

Giovanni Barbarossa
Giovanni Barbarossa 2020 年 3 月 29 日
Thank you for the quick reply! The dateinfo line of code generates a 31x1 cell array, and each cell contains a1xN datetime array (where N is different for each array)). Shouldn't we rather generate a Nx1 datetime array? In any case, the dateinfo line of code does not generate any error. Then the TT line of code returns the following:
Unable to assign into Uniform output array 1 at index 1. Set 'UniformOutput' to
false.
Caused by:
Subscripting into a table using one subscript (as in t(i)) or three or more
subscripts (as in t(i,j,k)) is not supported. Always specify a row subscript and
a variable subscript, as in t(rows,vars).
Thank you!
Walter Roberson
Walter Roberson 2020 年 3 月 29 日
dateinfo = cellfun(@(S) datetime({S.date}).', C, 'uniform', 0);
TT = cellfun(@(S, T) table2timetable(struct2table(S), 'RowTimes', T), C, dateinfo, 'uniform', 0);
Giovanni Barbarossa
Giovanni Barbarossa 2020 年 3 月 29 日
Thank you very much! Now it works but it leaves behind the previous 'date' field with dates in text format. How can I remove the previous 'date' field from all timetables? Thanks again
Giovanni Barbarossa
Giovanni Barbarossa 2020 年 3 月 29 日
I got it
TTT = cellfun(@(S) removevars(S,'date'), TT, 'uniform', 0);
Thank you again

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by