How to define variables once before 'for iterator subsystem' in Simulink

2 Ansichten (letzte 30 Tage)
Hello, I would like to create a Simulink model for C code generation (Raspberry Pi, ARM compatible). Therefore all variables in MATLAB function blocks have to be defined before first use. I would do it like this in MATLAB:
function [a, b] = fcn(count)
a = zeros(count, 1);
b = zeros(count, 2);
for i1 = 1:count
a(i1) = ...;
b(i1, :) = ...;
end
In Simulink the for iteration subsystem is used. Inside the block there is MATLAB function block which calculates 'a' and 'b'.
function [a, b] = fcn(count, i1)
a(i1) = ...;
b(i1, :) = ...;
end
Now the problem is that I don´t now where to define 'a' and 'b', so that the variables are not set to zero at every iteration cycle. Maybe the memory block is a solution but I don´t know how to use it in this case. Do you have any idea to solve this issue?
The next problem is that I have to define upper bounds for the variables, otherwise it throws an error at 'zeros(count, 1);' because the value of 'count' changes.
Thank you in advance for your help!
  2 Kommentare
Elias
Elias am 13 Apr. 2018
I solved it for me with the 'Memory' block which loops the output of the 'MATLAB function' block to the input of the same, all inside the 'For Iteration Subsystem'. The data have to be fixed size for use with the Memory block. Inside the MATLAB function block an if statement proves if the iterator variable is one and sets the output variables to zero before further processing. Else the variables take their init data from the Memory input.
Bhavanithya Thiraviaraja
Bhavanithya Thiraviaraja am 14 Mai 2018
Bearbeitet: Bhavanithya Thiraviaraja am 14 Mai 2018
For the first problem, if you can replace the matlab function block with simulink blocks, you can use the Memory or delay block to use the a and b values from the previous Iteration.
If you cannot replace it, then you can declare a and b as 'Persistent' variables which will retain its values throughout the execution. https://de.mathworks.com/help/matlab/ref/persistent.html
Please note that the Persistent values cannot be sent as output from a MATLAB function, so you may have to save it to another variable which is not persistent in order to output it.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Bhavanithya Thiraviaraja
Bhavanithya Thiraviaraja am 14 Mai 2018
For the second problem,
Upperbound for the variables can be given using the 'Model Explorer' in the view tab. The explorer shows a list of all the subsystems in the model and the variables used. You can specify the upperbound size in the 'size' field and click 'variable size'.

Kategorien

Mehr zu Simulink Coder finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by