Converting nested "summary" structure to table

14 ビュー (過去 30 日間)
Flynn McGuire
Flynn McGuire 2020 年 6 月 24 日
コメント済み: Flynn McGuire 2020 年 6 月 25 日
Hello,
I'm trying to convert a nested structure (full of table summaries) back into a table.
filled with
I have tried using Nested2tables but I don't think it will work given the single the nested structure.
Is there any way to create a table of the summary data while keeping the fieldnames from BOTH structures?
Thank you!
  2 件のコメント
Sindar
Sindar 2020 年 6 月 25 日
So, are you expecting a table like this?
table_name size Type ...
Days_from_development [118,1] 'double' ...
HIV [52,3] 'string' ...
Flynn McGuire
Flynn McGuire 2020 年 6 月 25 日
exaclty!

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

採用された回答

Sindar
Sindar 2020 年 6 月 25 日
編集済み: Sindar 2020 年 6 月 25 日
Here's the loop version. There may be another way, but I didn't have any luck fighting with Matlab's tricky multiple-cell return behaviour
% create minimal test structure
mystruct.Days_from_development = struct('size',[118,1],'Type','double');
mystruct.HIV = struct('size',[52,3],'Type','string');
% get a list of all the fieldnames (i.e. substructures, i.e. names of tables)
table_names = fieldnames(mystruct);
% loop over substructures
for ind=1:length(table_names)
% get the string for each substructure's name
tmp = table_names{ind};
% dump the substructure to a table row
% by turning it into a 1xN cell array and putting the substructure string as the first cell
mytable(ind,:) = cell2table([{tmp} struct2cell(mystruct.(tmp))']);
end
% rename the table variables appropriately
mytable.Properties.VariableNames = [{'table_name'};fieldnames(mystruct.(tmp))]
  2 件のコメント
Sindar
Sindar 2020 年 6 月 25 日
(this method is not particularly stable. If you have fields that are not all matching substructures, it will error)
Flynn McGuire
Flynn McGuire 2020 年 6 月 25 日
Unfortunatley I do have feilds that don't match. But this works well for everything else. Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by