how to concatenate tables stored in a structure

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:
T=cat(1,T1,T2,T3,T4,T5)
but want to do it with listing struct fields and concatenate.

댓글 수: 1

Stephen23
Stephen23 2018년 5월 11일
편집: Stephen23 2018년 5월 11일
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:
cat(1,S.T)
Putting numbers into fieldnames just makes everything more complicated.

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

 채택된 답변

Guillaume
Guillaume 2018년 5월 11일

4 개 추천

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

+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{:})

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

추가 답변 (2개)

Peter Perkins
Peter Perkins 2019년 5월 3일

1 개 추천

If you really ever only have five fields/tables in your scalar struct, just do this:
T=cat(1,S.T1,S.T2,S.T3,S.T4,S.T5)
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
{[1]}
{[2]}
{[3]}
>> vertcat(c{:})
ans =
1
2
3
John BG
John BG 2018년 5월 11일
편집: John BG 2018년 5월 11일

0 개 추천

Hi effess
there's no need to use the command cat or vertcat.
just concatenate the tables directly, like in the following example
1.
generating example data
var1=[1 2 3];var2=[4 5 6]
T1=table(var1,var2)
T2=T1
T3=T1
S(1).T=T1
S(2).T=T2
S(3).T=T3
2.
Since you clearly mentioned that all tables have the same variables, just put them all together like this:
T1=[S(1).T;S(2).T;S(3).T]
=
3×2 table
var1 var2
___________ ___________
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
.
effess
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

there's no need to use the command cat or vertcat
Note that [a, b] is just a short hand for horzcat. When you write [a, b], matlab ends up invoking horzcat(a, b).
John BG
John BG 2018년 5월 17일
same variables, the tables have the same variables. Of course you can do many different things to put together tables, but I show the simple way that is easiest to use when: both tables have exactly the same variables.
Jan
Jan 2018년 5월 17일
John, you wrote "there's no need to use the command cat or vertcat" and suggested [S(1).T;S(2).T;S(3).T]. But [;] and vertcat are completely equivalent and just a different notation.
Stephen's suggestion cat(1, S.T) is easier and does not depend on the number of fields.
To effess
Actually the generation of the structure as well as the concatenation can be simplified and automated with the powerful and well known command evalin:
Example
.
var1=[1 2 3];var2=[4 5 6]
T1=table(var1,var2)
T2=T1
T3=T1
var1=var1*10;var2=var2*11
T10=table(var1,var2)
table_names={'T1' 'T2' 'T3' 'T10'}
1.- structure storage
str_struct=[]
for k=1:1:size(table_names,2)
str_struct=[str_struct 'S(' num2str(k) ').T=' table_names(k) ';']
end
str_struct=cell2mat(str_struct)
evalin('base',str_struct)
.
check
.
S(1).T
S(2).T
S(3).T
S(4).T
=
1×2 table
var1 var2
___________ ___________
1 2 3 4 5 6
=
1×2 table
var1 var2
___________ ___________
1 2 3 4 5 6
=
1×2 table
var1 var2
___________ ___________
1 2 3 4 5 6
=
1×2 table
var1 var2
_________________ _________________
100 200 300 484 605 726
.
2.- Concatenation
.
str1=[];
for k=1:1:size(table_names,2)
str1=[str1 table_names{k} ';'];
end
str1(end)=[]
str1=['Tresult=' '[' str1 ']']
evalin('base',str1)
.
check
Tresult
=
4×2 table
var1 var2
_________________ _________________
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
100 200 300 484 605 726
.
effess
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
John BG
John BG 2018년 5월 19일
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.
Regards
John BG
Jan
Jan 2018년 5월 19일
편집: Jan 2018년 5월 19일
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
Jan 2019년 4월 30일
편집: Jan 2019년 4월 30일
@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.

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

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

제품

질문:

2018년 5월 11일

답변:

2019년 5월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by