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

조회 수: 2 (최근 30일)
Elias
Elias 2018년 4월 11일
편집: Bhavanithya Thiraviaraja 2018년 5월 14일
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
Elias
Elias 2018년 4월 13일
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 2018년 5월 14일
편집: Bhavanithya Thiraviaraja 2018년 5월 14일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Bhavanithya Thiraviaraja
Bhavanithya Thiraviaraja 2018년 5월 14일
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'.

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by