I'm looking for a very simple method to sum structures
which have identical substructure hierarchies.
Note: This began with an attempt to sum bus structures in Simulink.
MWE
Assume I have 2 structures red
and blue
.
Each structure has subfields a
, b
, and c
.
Each subfield has subfields x1
and x2
.
This is drawn below:
red. [a, b, c].[x1, x2]
blue.[a, b, c].[x1, x2]
Does a function exist such that a mathematical operator
may be applied to each parallel element of red
and blue
?
Something along the lines of:
purple = arrayfun( @(x,y) x+y, red, blue)
Realizing that dynamic code which does support arbitrary structs and supports code generation is impossible (afaik), the best possibility to simplify coding is a function which generates the code:
You call
structm2
inputting a struct and it returns the code: