问题
I am seeking a method to sum multilevel bus signals
at the lowest level within their hierarchy
without loss of the bus signal structure.
MWE
For example, in the MWE below,
the bus signal is summed at the lowest level automatically,
however, the bus signal is lost after passing through the Add block.
A vector signal is produced instead.
I would have preferred a method which yields:
red + blue = purple
or more specifically:
red.[a b c].[1 2] + blue.[a b c].[1 2] --> purple.[a b c].[1 2]
where
purple.b.3 = red.b.3 + blue.b.3
and so forth.
回答1:
To my knowledge, the only possibility to do math with bus signals is using a MATLAB Function block. Be aware of the potentially bad performance!
To use a MATLAB Function block, first generate a matching bus object and make sure the two bus creators mergin a / b / c each are assigned to output this bus object. Doing so you will receive an error because 1 and 2 aren't valid field names, i changed them to x1 and x2.
In the next step create a MATLAB Function Block. Set the two inputs and one output to your previous defined bus object. Paste the following code:
function y = fcn(u, v)
%#codegen
y.a=innerplus(u.a,v.a);
y.b=innerplus(u.b,v.b);
y.c=innerplus(u.c,v.c);
end
function y=innerplus(u,v)
y.x1=u.x1+v.x1;
y.x2=u.x2+v.x1;
end
来源:https://stackoverflow.com/questions/35851682/simulink-perform-math-operations-with-multilevel-bus-signals