How to create table from struct array

조회 수: 9 (최근 30일)
Doaa Alamoudi
Doaa Alamoudi 2021년 4월 27일
댓글: Doaa Alamoudi 2021년 4월 28일
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.

채택된 답변

Stephen23
Stephen23 2021년 4월 27일
편집: Stephen23 2021년 4월 27일
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)
S = struct with fields:
Mar23_2021: [1×1 struct] Apr2_2021: [1×1 struct] Apr10_2021: [1×1 struct]
F = fieldnames(S);
D = datetime(F,'InputFormat','MMMd_yyyy');
C = struct2cell(S);
T = struct2table(vertcat(C{:}));
T.date = D
T = 3×3 table
A B date __ __ ___________ 11 12 23-Mar-2021 21 22 02-Apr-2021 31 32 10-Apr-2021
  댓글 수: 1
Doaa Alamoudi
Doaa Alamoudi 2021년 4월 28일
Thanks for your answer. The answer is for creating table, however, I have the table ready imported from JSON file and I would like to extract the data and analyze it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by