I am trying to make a level 2 s-function which would act like a buffer (I just started learning S-functions). Now what I want is that every time an input comes in it gets stored in the next index until the buffer becomes full then it starts to push the data stored from 2nd till last index to 1st till second last index and updates itself after the sampling time I will attach my code what would be relevant for better understanding
function Buffer(block)
global i;
i = 1;
setup(block);
function setup(block)
% Register number of ports
block.NumInputPorts = 2;
block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Override input port properties
block.InputPort(2).Dimensions = 1;
block.InputPort(2).DatatypeID = 0; % double
block.InputPort(2).Complexity = 'Real';
block.InputPort(2).DirectFeedthrough = true;
% Override input port properties
block.InputPort(1).Dimensions = 1;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = true;
% Override output port properties
block.OutputPort(1).Dimensions = [1 block.InputPort(2).Data];
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.SampleTimes = [-1 0];
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('Outputs', @Outputs);
block.RegBlockMethod('Update', @Update);
block.RegBlockMethod('Terminate', @Terminate);
function Outputs(block)
block.OutputPort(1).Data(i) = block.InputPort(1).Data;
% block.Dwork(1).Data
%end Outputs
%%
%% Update:
%% Functionality : Called to update discrete states
%% during simulation step
%% Required : No
%% C-MEX counterpart: mdlUpdate
%%
function Update(block)
if(i == block.InputPort(2).Data)
block.OutputPort(1).Data(1:block.InputPort(2).Data - 1) = block.OutputPort(1).Data(2:block.InputPort(2).Data);
else
i = i + 1;
end
%end Update
function Terminate(block)
Now the problem is although I have declared i
as a global variable , when I run the function it says i is undefined , does anyone know what am I doing wrong ?