How to merge multiple structures into one structure or a table?

6 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2022-2-9
编辑:Stephen23 2022-2-9
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by