Main Content

Component Arrays

About Component Arrays

Component arrays provide an intuitive way to model composite components with an arbitrary number of homogeneous members, such as segmented pipelines, battery packs, or transmission lines.

Use a for loop to declare an array of member components:

  for i=1:array_size
     components (ExternalAccess=none)
	member_comp(i) = compX;
     end
  end

The array size can be declared as an adjustable parameter of the composite component, so that the block users can modify this value.

  parameters
     array_size = 10; 		      % Number of member components
  end

Declare the parameter specifying the array size as a unitless integer because a for loop iterator must be a unitless integer.

Similar to regular composite components, if you want certain parameters of the underlying member component to be adjustable through the composite component interface, include them in the member declaration. This example establishes the relationship between parameter parX of the member component compX and the top-level parameter top_level_parX of the composite component:

  parameters
     array_size = 10;              % Number of member components
     top_level_parX = { 1, 'm' };  % Modifiable parameter of the member components
  end
  for i=1:array_size
     components (ExternalAccess=none)
	member_comp(i) = compX(parX = top_level_parX);
     end
  end

Use for loops to specify connections between the member components. The iterator range for these for loops depends on the array size and the type of connection. For example, when you connect N members in parallel, the iterator range is equal to the array size:

  for i=1:N
     connections
	connect(compX(i).A, A);
	connect(compX(i).B, B);
     end
  end

However, if you connect N members in series, the iterator range is from 1 to (N-1), because you are connecting port B of each member except the last one to port A of the next member:

  for i=1:(N-1)
     connections
	connect(compX(i).B, compX(i+1).A);
     end
  end

In this case, do not forget to connect the ends of the chain to the external ports of the composite component:

  connections
     connect(compX(1).A, A);
     connect(compX(N).B, B);
  end

You can also use compX(end) to indicate the last member of a component array. For example, this syntax is equivalent to the previous one, for connecting the ends of the chain to the external ports of the composite component:

  connections
     connect(compX(1).A, A);
     connect(compX(end).B, B);
  end

You can use nested for loops to create multidimensional arrays of components.

Syntax Rules and Restrictions

The following rules and restrictions apply to arrays in Simscape™ language:

  • Arrays apply only to the component member class.

  • Component arrays must be homogeneous, that is, their members must all belong to the same class. However, members can have different parameter values.

  • The array size can be a parameter, or a parametric expression. Parameters that control the array size can have their ExternalAccess attribute set to modify, which enables the block users to change the size of the array.

  • Array members must have the ExternalAccess attribute set to none or observe.

  • Empty arrays are not supported.

You can use for loops to declare component arrays and to connect members of the array to each other. for loops have the same syntax as for in MATLAB®. The following rules and restrictions apply to for loops in Simscape language:

  • for loops can contain only components or connections.

  • The for loop iterator must be a unitless integer.

  • for loops can be nested. Use nested for loops to create multidimensional arrays of components.

  • In nested for loops, the iterator in a nested loop cannot refer to an iterator in a loop above it. For example, this syntax is invalid:

    for i=1:N
       for j=1:i
         ...
       end
    end
    
  • Component declaration using a for loop must contain the for loop iterator on the left side as a bare identifier, for example, pipe(i). You cannot use expressions or numbers in place of an iterator. Components declared inside a nested for loop must list all the iterators, for example:

    for i=1:N
       for j=1:M
         components (ExternalAccess=none)
    	resistor(i,j) = foundation.electrical.elements.resistor(R = R);
         end
       end
    end
    
  • You cannot include conditional sections (that you use to define component variants) inside for loops. However, you can include for loops inside the conditional sections.

Related Topics