Concatenate structure with subfields
2 次查看(过去 30 天)
显示 更早的评论
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 个)
John Chilleri
2017-2-22
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 个评论
Jan
2017-2-23
@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
2017-2-24
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!