MATLAB Answers

how to synchronize structures with date fields in text format

2 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2020
dateinfo = cellfun(@(S) datetime({S.date}), C, 'uniform', 0);
TT = cellfun(@(S, T) table2timetable(struct2table(S), 'RowTimes', T), C, dateinfo);

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 29 Mar 2020
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 on 29 Mar 2020
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 on 29 Mar 2020
I got it
TTT = cellfun(@(S) removevars(S,'date'), TT, 'uniform', 0);
Thank you again

Sign in to comment.

More Answers (0)

Sign in to answer this question.