How to merge multiple structures into one structure or a table?
8 views (last 30 days)
Show older comments
I have an existing structure 'result1' which has several fields (10 in number) which are structures too. Please check the image attached. Inside each of these fields, say 'result(1).values' there are 5 different values and non-uniform in length compared result(2).values and so on. To proceed with my work I need to merge all the structures in one of the fields (example values field in the structure result1) into one structure. Based on my level of understanding so far, the only option seems to be working seems to be the below command. However, I have hundreds of entries in the field to be merged and this command based method is definately not the smartest one to do. Could anyone please help me on this?
Merged = [result1(1).values,result1(2).values,result1(3).values,result1(4).values.......till the last value];
Please note I tried vertcat and that doesn't work as there multiple fields side sub structures.
0 Comments
Accepted Answer
Stephen23
on 9 Feb 2022
Edited: Stephen23
on 9 Feb 2022
The MATLAB approach, these are all equivalent:
merged = [result1.values];
merged = cat(2,result1.values);
merged = horzcat(result1.values);
"Please note I tried vertcat and that doesn't work as there multiple fields side sub structures."
VERTCAT will not work anyway, as your nested structures (i.e. fields PULSES, PULSES_F, VALUES, CHARGEPULSE and DISCHARGEPULSE) mostly** have size 1xN with different N, so they cannot be concatenated vertically as you attempted. But of course they can be concatenated horizontally (as my code above shows).
The fields with DOUBLE data (TIME, POWER, SOC, T, F) all have sizes Nx1, for which of course you will need to concatenate them vertically:
merged = cat(1,result1.values);
merged = vertcat(result1.values);
You cannot just randomly guess which dimension to concatenate along, you need to concatenate depending on the sizes of the arrays that you are concatenating together.
** some are empty, size 0x0. I will check their concatenation behavior shortly.
More Answers (0)
See Also
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!