每次我的MATLAB函数被调用时,它都是无状态的,所以我只有输入变量的值。如何管理周期之间的状态(即变量的值)?例如,在步骤100上,我做了一些计算,需要在步骤200上使用。我本可以使用全局变量,但它们不受支持。
发布于 2015-11-01 20:01:47
这就是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;https://stackoverflow.com/questions/33463146
复制相似问题