Make a table from a structure

20 ビュー (過去 30 日間)
Milton Logothetis
Milton Logothetis 2019 年 1 月 2 日
回答済み: Peter Perkins 2019 年 1 月 2 日
Hi, I want to create a table from a structure that has n fields and I also want the fieldnames to be added as variable names. I have tried the following but I can't find a way to change the column when each field is added:
fnames = fieldnames(data); % get fieldnames
T = table; %assign table to T
for i = 1:length(fnames)
x_T = table(num2cell(getfield(data, fnames{i}))); %extract each field into a variable and convert it to a cell array
T = [T; x_T];
end
T.Properties.VariableNames = {fnames{1:i}};
  1 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 2 日
upload your datas

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

採用された回答

Milton Logothetis
Milton Logothetis 2019 年 1 月 2 日
I figured it out ! Apparently the field name should be set to x_T before inserting the variables to the table.
fnames = fieldnames(data);
T = table;
for i = 1:length(fnames)
x_T = table(num2cell(getfield(data, fnames{i})));
x_T.Properties.VariableNames = {fnames{i}};
T = [T, x_T];
end

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2019 年 1 月 2 日
Milton, I think you are looking for struct2table.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by