MATLAB: How to implement a looping counter with MATLAB Fcn Blocks

simulink

How do I implement a looping counter with MATLAB Fcn Blocks?

Best Answer

You can use the "persistent" keyword inside the MATLAB Fcn Block to implement a counter that resides entirely within this block.
Then, in order to loop the counter back, you want to use a remainder function.
An example code snippet below:
function y = fcn(u)
persistent cnt;
if isempty(cnt)
cnt = 0;
end
cnt = cnt +1;
cnt = rem(cnt,72);
y = A(:,cnt)
end