SIMULINK:在嵌入式 matlab 函数中管理(保存)变量状态

SIMULINK: Managing (saving) variable state in embedded matlab function

每次调用我的 MATLAB 函数时,它都是无状态的,因此我只有输入变量的值。我如何管理周期之间的状态(即变量值)?例如,在第 100 步我做了一些计算,我需要在第 200 步使用它。我会使用全局变量,但它们不受支持。

这就是 persistent 变量的用途。 有关详细信息,请参阅 >>doc persistent,但基本上您需要以下内容

function y = fcn(u)

%define persistent variables
persistent a b c

% initialize persistent variables (at t=0)
if isempty(a)
   a = 1;
   b = 10;
   c = 12;
end


% update variables
a = a+7;
b = b+4;

% update out
y = u + a + b + c;