Why do cascaded Assignment blocks seem to behave incorrectly in a For Iterator subsystem?

3 Ansichten (letzte 30 Tage)
I am trying to write values to a matrix signal in Simulink. To do this, I cascaded Assignment blocks and put them inside a For Iterator subsystem. However, the output does not look as I expected it. Is this a bug?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 9 Apr. 2021
The behavior may seem like a bug at first glance, but it is the expected behavior of the Assignment block in iterated assignment mode. It is documented in the following example:
The following happens in one time step for one Assignment block:
  • On the first iteration (i = 1), the Assignment block copies the first input Y0 to the output (Y) and assigns the second input (U) to the output Y.
  • On successive iterations (i = 2,...,N), the Assignment block assigns the current value of U to Y, without first copying Y0 to Y. This means that after the first iteration, any changes made to the input Y0 will not be reflected in the output. Instead, the previous value of the output vector is taken for the next iterations.
That explains why you get the observed output when you cascade Assignment blocks in iterated assignment mode as shown in the above screenshot. During the first iteration (i=1), the following happens:
(1) The first Assignment block copies the matrix of zeros (Y0) to the output (Y) and assigns the value 1 to index (1,3). The output is [0 0 1; 0 0 0; 0 0 0].
(2) The second Assignment block copies the output from the first Assignment block [0 0 1; 0 0 0; 0 0 0] and assigns the value 2 to index (1,2). The output is [0 2 1; 0 0 0; 0 0 0].
(3) The third Assignment block copies the output from the second Assignment block [0 2 1; 0 0 0; 0 0 0] and assigns the value 3 to index (1,1). The output is [3 2 1; 0 0 0; 0 0 0].
At the end of this first iteration, the propagation of the matrix signal has finished. Any changes in the first and second assignment block outputs after the first iteration will not be reflected in the third assignment block. On successive iterations, the third Assignment block will continue writing values to [3 2 1; 0 0 0; 0 0 0], resulting in the final output [3 2 1; 3 0 0; 3 0 0].
To work around this, you will need to take a different approach such as using a MATLAB Function Block, or a For Each subsystem. See the attached model for some ideas.

Weitere Antworten (0)

Kategorien

Mehr zu Subsystems finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by