Using a persistent array in a Simulink function block

조회 수: 8 (최근 30일)
richard
richard 2023년 3월 27일
답변: Paul 2023년 3월 27일
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];

채택된 답변

Paul
Paul 2023년 3월 27일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by