| Simulink Reference | ![]() |
Assign values to specified elements of a signal
Library
Description
The Assignment block assigns values to specified elements of the signal. You can specify the indices of the elements to be assigned values either by entering the indices in the block's dialog box or by connecting an external indices source or sources to the block. The signal at the block's data port, labeled U2 in most modes, specifies values to be assigned to Y. The block replaces the specified elements of Y with elements from the data signal, leaving unassigned elements unchanged from their initial values. If the assignment indices source is internal or is external and the Initialize using input option is selected, the Assignment block uses the signal at the block's initialization port, labeled U1, to initialize the elements of the output signal before assigning them values from U2.
You can use the block to assign values to vector, or matrix signals.
Assigning Values to a Vector Signal
To assign values to a scalar or vector signal, set the block's Input Type parameter to Vector. The block's dialog box displays a Source of element indices parameter. You can specify the indices source as Internal or External. If you select Internal, the block dialog box displays an Elements field. Use this field to enter the element indices. If you specify External as the source of element indices, the block displays an input port named E. Connect an external index source to this port. Use Index mode to specify whether 0 or 1 indicates the first element of Y.
The index source can specify any of the following values as indices:
If Use index as starting value option is not selected, the block assigns U2, which must be a scalar, to the specified element of Y.
If Use index as starting value is selected, the block assigns U2 to a range of elements of Y, starting at the specified index. For example, suppose that U1 is a 5-element vector, U2 is a 3-element vector, the index mode is one-based, and the starting index is 3. In this case, the Assignment block assigns U2(1:3) to Y(3:5).
Assigns U2 to a specified set of elements of Y.
The width of the values signal connected to U2 must be the same as the width of the indices vector. For example, if the indices vector contains two indices, U2 must be a two-element vector of values. The block assigns the first element of U2 to the element of Y specified by the first index, the second element of U2 to the Y element specified by the second index, and so on.
If U2 is a scalar, it is assigned to the specified elements of the output vector.
Assigning Values to a Matrix Signal
To assign values to a matrix signal, set the Input Type parameter to Matrix. If you specify the Input Type of the Assignment block as Matrix, the block's dialog box displays a Source of row indices parameter and a Source of column indices parameter. You can specify either or both of these parameters as Internal or External. If you specify the row and/or column index source as internal, the block displays a Rows and/or Columns field. Enter the row or column indices of the elements of Y to be assigned values into the corresponding field. If you specify the row and/or column index source as External, the block displays an input port labeled R and/or an input port labeled C. Connect an external source of indices to each indices port.
A row or column indices source can have any of the following values:
If Use index as starting value option is not selected, the block assigns values to the specified row or column. If Use index as starting value is selected, the block assigns values from U2 to a range of rows or columns of Y, starting at the specified row or column index. For example, suppose that U1 is a 5 x 5 matrix, U2 is a 3 x 3 matrix, the indexing mode is one-based, and the starting row and column indices are both 3. In this case, the Assignment block assigns U2(1:3, 1:3) to Y(3:5,3:5).
Specifies a set of rows or columns of Y.
The block assigns values from U2 to the specified elements of Y in column-major order. In particular, the block assigns the first element of the first row of U2 to the first specified element in the first specified row in Y. It assigns the second element of the first row of U2 to the second specified element of the first specified row of Y, and so on.
To enable all specified elements to be assigned values, U2 must be an N-by-M matrix where N is the width of the row indices vector and M is the width of the column indices vector. For example, suppose that you specify a vector of row indices of size 2 and a vector of column indices of size 4. Then U2 must be a 2-by-4 matrix signal.
When determining the dimensions of U2, count a scalar index as a vector of size 1 and -1 as equivalent to a vector of indices of the same width as the row or dimension size of Y. For example, suppose your row and column index sources are a scalar and a two-element vector, respectively. Then U2 must by a 1-by-2 matrix.
If U2 is a scalar, the Assigment block assigns the scalar to the specified elements of the output signal.
Iterated Assignment
You can use the Assignment block to assign values computed in a For or While Iterator loop to successive elements of a vector or matrix signal in a single time step. For example, the following model uses a For Iterator block to create a vector signal each of whose elements equals 3*i where i is the index of the element.
Iterated assignment uses an iterator (For or While) block to generate the indices required by the Assignment block. On the first iteration of an iterated assignment, the Assignment block copies the first input (U1) to the output (Y) and assigns the second input (U2) to the output Y(E0). On successive iterations, the Assignment block simply assigns the current value of U2 to Y(Ei), i.e., without first copying U1 to Y. All of this occurs in a single time step.
Data Type Support
The data and intitialization ports of the Assignment block accept signals of any data type supported by Simulink, including fixed-point data types. The external indices port accepts any data type, except boolean and fixed-point data types.
For a discussion on the data types supported by Simulink, refer to Data Types Supported by Simulink in the Using Simulink documentation.
Parameters and Dialog Box
Vector or Matrix input. If you select Vector, the Source of element indices field appears. If you select Matrix, the Source of row indices and Source of column indices fields appear.
0 or 1.
Internal (the default) or External as the source for the indices of the elements to be assigned values. If you select Internal, the block dialog box displays an Elements field (see following). Use this field to enter the element indices. If you select External, the block displays an input port labeled E. Connect the external index source to this port.
Internal for the Source of element indices field. It specifies the indices of elements in Y to be assigned values from elements in U2. The value of this parameter can be -1, a nonnegative integer specifying a single index, or a vector of nonnegative integers specifying a set of indices (e.g., [1,3,5,6]).
Internal (the default) or External. If you select Internal, the Rows field appears. Enter the indices of the rows to be assigned values in this field. If you select External, the block displays an input port labeled R. Connect an external source of row indices to this port.
Internal for the Source of row indices field. Valid values are -1 (all rows), a single row index, or a vector of row indices (e.g., [1,3,5,6]).
Internal (the default) or External. If you select Internal, the Columns field appears. Enter the indices of the columns to be assigned values in this field. If you select External, the block displays an input port labeled C. Connect an external source of column indices to this port.
internal for the Source of column indices field. Valid values are -1 (all columns), a single column index, or a vector of column indices (e.g., [1,3,5,6]).
Initialize using input (U1) (the default) or Specify required dimensions. The first option causes the Assignment block to display an initialization port labeled U1 and to use the signal at this port to initialize the output signal (Y) before assigning it values from the data port (U2) as specified by the external indices signal (E). The second option does not initialize Y before assigning values from the block's data input port (labeled U1 in this case) to it. This option requires that the block's U1 and E inputs assign values to every element of Y. Further, it requires that you specify the dimensions of the output signal (see next control).
Specify required dimensions option of the Output (Y) control. It specifies the dimensions of the Assignment block's output signal.
Specify required dimensions option of the Output (Y) control. It specifies the diagnostic action that Simulink should take if the block's data (U1) and external indices (E) inputs do not assign a value to every element of the block's output (Y). The options are to display an error message and halt the simulation (Error), display a warning message (Warning) and continue the simulation, or continue the simulation (None). If you choose Warning or None, the values of the unassigned elements of the output are undefined.
-1. See Specifying Sample Time in the online documentation for more information.
Characteristics
| Direct Feedthrough |
Yes |
| Sample Time |
Inherited from driving block |
| Scalar Expansion |
Yes |
| Dimensionalized |
Yes |
| Zero Crossing |
No |
| Assertion | Backlash | ![]() |
© 1994-2005 The MathWorks, Inc.