MATLAB Answers

Concatenate structure with subfields

5 views (last 30 days)
M
M on 21 Feb 2017
Commented: John Chilleri on 24 Feb 2017
I have a 1x1 structure which has 1x3 fields which further has 1x5 runs. Each run has different double data(same no. of columns, different no. rows). I want to concatenate it like run 1 its data 203x7 double, run 2 its data 216x7 double and so on. Also assign Column names to each 7 columns. How to do that within a loop?

  1 Comment

Jan
Jan on 21 Feb 2017
Can you post a small example? What does "1x5 runs" exactly mean?

Sign in to comment.

Answers (1)

John Chilleri
John Chilleri on 22 Feb 2017
Hello,
Hopefully I understand your question properly.
You can concatenate them with:
data = [datarun1; datarun2; ...; datarun5];
Furthermore, the easiest way to have column names is to have a separate variable (1x7) containing the names, unless you want to look into cells, in which you can have column names at the top before the data. There might exist a better way for column names, but these are some simple approaches.
Hope this helps!

  2 Comments

Jan
Jan on 23 Feb 2017
@M: Screenshots are cruel in the forum: We can see the data, but to use them all readers have to type a lot of boring code. Please post a small example of the input in valid Matlab syntax. Then it will be much easier to provide an answer and modify it until it matchs your needs.
John Chilleri
John Chilleri on 24 Feb 2017
Hello,
Perhaps you can make a new field in your struct with the concatenated data:
>> a.b.c = rand(4,3)
a =
b: [1x1 struct]
>> a.b.d = rand(5,3)
a =
b: [1x1 struct]
>> a.b.e = [a.b.c; a.b.d];
>> a.b.e
ans =
0.0462 0.3171 0.3816
0.0971 0.9502 0.7655
0.8235 0.0344 0.7952
0.6948 0.4387 0.1869
0.4898 0.2760 0.4984
0.4456 0.6797 0.9597
0.6463 0.6551 0.3404
0.7094 0.1626 0.5853
0.7547 0.1190 0.2238
(Concatenated a 4x3 with a 5x3 in new field a.b.e.). Is this closer to what you're needing?

Sign in to comment.

Tags

No tags entered yet.