Using MATLAB Functions with Arrays of Components and Nodes
Simscape™ language allows you to use certain MATLAB® functions on arrays of components and nodes to concatenate and reshape these
arrays without using for
-loops. You can also query an object array size and
then use that size in a parametric expression in this or another object array.
The supported functions are:
Array manipulation:
repmat
,cat
,horzcat
,vertcat
,reshape
,transpose
. These functions perform manipulations on the input object array and return an object array.Array query:
size
,numel
,ndims
. These functions query the size aspects of an object array and return a result of primitive data type. These functions can be evaluated only at compile time.
Rules and restrictions:
Only arrays of the same object type can be concatenated. For example, you cannot concatenate an array of electrical nodes and an array of thermal nodes.
All arguments that specify size or dimension must be unitless constants or unitless compile-time parameters.
When using
reshape
, expanded empty dimension is not supported. This restriction is consistent with using thereshape
function in theequations
section.When using
size
, the second argument must be a scalar. It can be a constant or a parametric expression. However, using a vector as the second argument or using more than two arguments is not supported.You cannot use the array query functions in the predicates of conditional declarations.
Array manipulation functions also allow you to connect arrays of components and arrays of
nodes without using for
-loops. For example, you can connect a scalar node
to an array of nodes:
component A parameters N = 3; end nodes n = foundation.electrical.electrical; end components c = repmat(foundation.electrical.elements.resistor, [1, N]); end connections connect(repmat(n,1,N), [c.p]); end end
Arrays of components and nodes are similar to MATLAB structure arrays. When you extract a field of a structure array across one of
its dimensions, the result is a comma-separated list. (For more information, see Generating a List from a Structure.)
Similarly, in arrays of components or nodes, referencing a member using dot notation returns a
comma-separated list. Therefore, to create element-wise connections without
for
-loops, you must use concatenation.
For example, if your model contains an array of resistors connected in series with an
array of current sensors, you can connect the negative node of each resistor to the positive
node of each sensor by using a for
-loop:
for i=1:length(R_array) connections connect(member_resistor(i).n,member_sensor(i).p); end end
The equivalent syntax without using a for
-loop requires concatenation:
connections connect([member_resistor.n],[member_sensor.p]); end
or
connections connect(cat(1,member_resistor.n),cat(1,member_sensor.p)); end
Because both member_resistor.n
and member_sensor.p
return comma-separated lists, connecting them without using concatenation,
connect(member_resistor.n,member_sensor.p)
, creates a common node instead
of element-wise connections.