Cannot vertically concatenate tables in for loop

Hello experts,
I have an elementary, yet unsolvable issue for me. I checked other questions, I checked the material on the official site but I cannot solve my problem.
I have a 1x1 structure named "tbl" with 17 fields. Each field is a Nx5 table.
I would like to take each table and append that table to the previous one to obtain an Nx5 table, where N is the total number of rows from all the tables.
To append one to the other I know I can do this:
total = [tbl.ID2; tbl.ID4];
But I'd rather do it in a for loop, since I'll add further IDs and it's going to be quite time consuming.
resulting_tbl = table;
for id = tbl
resulting_tbl = [id;];
end
this code above is just creating the same initial structure instead of a new resulting table.
I know I'm missing some basic notions, but I cannot overtake this problem.
Thank you all for your time and help!
Marco

4 件のコメント

KSSV
KSSV 2021 年 2 月 8 日
Read about join.
NMarco
NMarco 2021 年 2 月 8 日
編集済み: NMarco 2021 年 2 月 8 日
Thank you @KSSV for your suggestion.
Of course I read about join but I'm definetly missing something because I'm not able to acheive what I'm trying to do. I'm getting error that they should be arrays or vectors to be joined together.
What am I missing here?
resulting_tbl = table;
for id = tbl
resulting_tbl = [id;]; % join here?
end
Thank you!
Stephen23
Stephen23 2021 年 2 月 8 日
@Marco Ninghetto: having lots of numbered fields is not very good data design. It would be better to use a non-scalar structure or even just a simple cell array.
NMarco
NMarco 2021 年 2 月 8 日
@Stephen Cobeldick, so I will probably change the names. I used them as they are because it's easier to trace back to which participant belongs that specific table.

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

 採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 8 日

0 投票

temp = struct2cell(tbl);
resulting_tbl = vertcat(temp{:});

3 件のコメント

NMarco
NMarco 2021 年 2 月 8 日
I tried that, but unfortunately:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
all the tables have different lenght.
Walter Roberson
Walter Roberson 2021 年 2 月 8 日
The image you posted showed all tables as having different heights but all of them had 5 variables. Are there non-empty entries that have a different number of variables?
temp = struct2cell(tbl);
wids = cellfun(@(C) size(C,2), temp);
[uwids, ~, uidx] = unique(wids);
counts = accumarray(uidx(:), 1);
[~, maxidx] = max(counts);
report_table = [counts, uwids];
report_table(maxidx,:) = [];
if isempty(report_table)
fprintf('Congrats! All cells have %d variables!\n', uwids(1));
else
fprintf('Opps! Not all cells have the same number of variables! Uncommon ones:\n');
fprintf('%d cells have %d variables\n', report_table.');
end
NMarco
NMarco 2021 年 2 月 8 日
Well, my bad. In the fifth column (should be group variable), some of the tables have a different format from the others. That's why it wasn't working. I fixed it.
The method:
temp = struct2cell(tbl);
resulting_tbl = vertcat(temp{:});
worked perfectly. Thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2021 年 2 月 8 日

コメント済み:

2021 年 2 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by