How to create table from struct array
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Doaa Alamoudi
am 27 Apr. 2021
Kommentiert: Doaa Alamoudi
am 28 Apr. 2021
I would like to create table from imported json table.
The data imported are arranged in struct array, So I ould like to create table by combining fields and value in one table. 

0 Kommentare
Akzeptierte Antwort
Stephen23
am 27 Apr. 2021
Bearbeitet: Stephen23
am 27 Apr. 2021
Note that mixing up meta-data (the date) into fieldnames forces superfluous nesting of structures. This poor data design has already allowed one bug into your data: dates with/without leading zeros has allowed duplicates, e.g. for 2021 both of "Apr1" and "Apr01" are listed, as are both of "Apr2" and "Apr02", and as are both of "Apr4" and "Apr04".
Much better data design would store the date as data in its own right: if you have the choice, store the data in an Nx1 structure with a date field (and store the date as datetime or a datevector or anything more robust than those strings).
But given what you have described:
F1 = struct('A',11,'B',12);
F2 = struct('A',21,'B',22);
F3 = struct('A',31,'B',32);
S = struct('Mar23_2021',F1, 'Apr2_2021',F2, 'Apr10_2021',F3)
F = fieldnames(S);
D = datetime(F,'InputFormat','MMMd_yyyy');
C = struct2cell(S);
T = struct2table(vertcat(C{:}));
T.date = D
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!