MATLAB Answers

how to concatenate tables stored in a structure

조회 수: 110(최근 30일)
DuckDuck 11 May 2018
답변: Peter Perkins 3 May 2019
I have a structure where tables with same variable names are stored. I want to concatenate all these tables in one big table.
say i have struct S
struct with fields:
T1: [34113×16 table]
T2: [34133×16 table]
T3: [34059×16 table]
T4: [33297×16 table]
T5: [34150×16 table]
I can do:
but want to do it with listing struct fields and concatenate.

  댓글 수: 1

Stephen Cobeldick
Stephen Cobeldick 11 May 2018
This would be a lot simpler with a non-scalar structure:
S(1).T = ...
S(2).T = ...
S(3).T = ...
then all you would need to do is:
Putting numbers into fieldnames just makes everything more complicated.

로그인 to comment.

채택된 답변

Guillaume 11 May 2018
As Stephen commented, the initial designed is flawed. numbered variables and field names should always be replaced by a single container.
With a structure it's easily fixed by converting the structure into a cell array:
C = struct2cell(S);
vertcat(C{:}) %vertically concatenate all the fields of S

  댓글 수: 1

Jan 17 May 2018
+1. If the data are represented as fields of a struct, this is the easiest solution. Stephen's comment showed, how to avoid a list of "numbered" fields by using a struct array. Alternatively you could store the tables in a cell array also: T{1}, T{2}, ... or if you need it: S.T{1}, S.T{2}, ... Then the concatenation is done again by:
vertcat(S.T{:}) % or vertcat(T{:})

로그인 to comment.

추가 답변(2개)

John BG
John BG 11 May 2018
편집: John BG 11 May 2018
Hi effess
there's no need to use the command cat or vertcat.
just concatenate the tables directly, like in the following example
generating example data
var1=[1 2 3];var2=[4 5 6]
Since you clearly mentioned that all tables have the same variables, just put them all together like this:
3×2 table
var1 var2
___________ ___________
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG

  댓글 수: 7

표시 이전 댓글 수: 4
John BG
John BG 19 May 2018
and when the tables have exactly the same variables, just in case there was any doubt, there's no need to use vertcat, or horzcat, or any cat of any type, the more compact semi colon notation works ok.
John BG
Jan 19 May 2018
Suggesting evalin('base') to create a struct array (in the base workspace!) is complete useless and a bad programming practice. Compare this with the 2 lines of code suggested by Guillaume, which are working inside a function also.
John, you insist on claiming that horzcat is not needed and do use it repeatedly in form of its abbreviation [;] . This is a slapstick comedy.
Jan 30 Apr 2019
@Rene Ferdinands: I've read your (meanwhile deleted) opinion about my "condescending" replies. Take into account, that this discussion has a background you are not aware of. Many contributions have been removed by the admins due to rudeness, so if you can see only my parts of the dicsussion, does not necessarily mean, that I was the aggressive part.
I appreciate that you care about the polite and friendly tone in the forum.

로그인 to comment.

Peter Perkins
Peter Perkins 3 May 2019
If you really ever only have five fields/tables in your scalar struct, just do this:
But presumably you already knew that and you have either a large number of fields/tables, or a variable number. Try this (which uses numeric scalars instead of tables, but it's exactly the same):
>> s = struct; s.A = 1; s.B = 2; s.C = 3
s =
struct with fields:
A: 1
B: 2
C: 3
>> c = struct2cell(s)
c =
3×1 cell array
>> vertcat(c{:})
ans =

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by