Using a persistent array in a Simulink function block

10 views (last 30 days)
I have a Simulink block that takes inputs, and remembers past inputs, in order to generate its outputs. Sometimes, the buffer to remember past inputs can be very large (20 in my case, but it could be as big as 50). I know I can use a "z" delay block to hold a past value, but I don't want to have to place 20 delay blocks on my Simulink block diagram.
Therefore, inside of my Simulink block, I am declaring this code (see below). My problem is that I get an error when running the model:
Parse error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters. Function 'DMD' (#37.187.188), line 6, column 18: "[" Launch diagnostic report.
The error above is compaining about my "States[21]" array - there are other errors as well that I did not include here.
--> How do I declare persistent arrays in my Simulink block, so that values in these arrays remain for the next time step in Simulink?
function [a11, a12, a21, a22, b1, b2] = fcn(I_in, V_out, U_in)
persistent States[21];
persistent U[21];
persistent Omega[20];

Accepted Answer

Paul
Paul on 27 Mar 2023
Hi richard,
The typical structure would look like this:
function [a11, a12, a21, a22, b1, b2] = fcn(I_in, V_out, U_in)
persistent States
persistent U
persistent Omega
if isempty(States)
States = zeros(21,1); % or whatever you want to initialize as
U = zeros(21,1);
Omega = zeros(20,1)
end
% rest of code goes here
end

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!