0

I have this simple system:

Simple System

The code in the function is:

function a = fcn(iteration,a)
a(1,iteration) = iteration;
end

The outside Simulink layer is the following (a is initialized as a = zeros(1,5)):

Upper layer

The loop is:

for i = 1:5

and I want to store the iterator in an array (i.e. [1 2 3 4 5]), but for some reason Simulink initializes the array at every iterator step for a single time step, so I can only see the last value. I have tried many different combinations, but I cannot make it work the way I want. How to do it?

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58

2 Answers2

0

With every iteration the MATLAB Function starts from the initial array, the input port. To prevent this, just add a loop with a delay. The output of the MATLAB Function should feed the delay as well as the output port. This ensures the result of the last iteration is also available. You can use the resettable delay with an external initial condition to start with the input value and update that during every iteration.

loop

  • Did this work in your Simulink? I tried it, but I get the following error: `When the input signal of 'test_of_iterations/For Iterator Subsystem/Resettable Delay' is variable-size, only scalar is allowed for initial value`. – Chris Patr May 30 '20 at 09:02
  • If I remove the Variable size option from the channel, I get the following: `When parameter 'InputProcessing' is set to 'Elements as channels (sample based)' and data input signal is 2-D or N-D signals, the dimension of initial condition should be scalar or equal to the dimension of data input if it is unit delay.` – Chris Patr May 30 '20 at 09:09
  • For me the delay worked, using R2020a, and having specified dimensions, but tapped delay seemed to work for you... – MonkeyProof Solutions Jun 02 '20 at 05:57
0

I found that there is a tapped delay option. With this option, you can store as a vector (dynamically) various values in Simulink.