1

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 ?

Novice_Developer
  • 1,432
  • 2
  • 19
  • 33
  • 1
    You should be using a DWork vectors, not globals. See [How to Use DWork vectors](https://www.mathworks.com/help/simulink/sfg/how-to-use-dwork-vectors.html#brd2qpw) to see how to do this. – Phil Goddard Aug 25 '16 at 05:01
  • @PhilGoddard Thankyou for the suggestion .... I realized this after a while and made a code out of it now my buffer function works perfectly , Thanks for the suggestion – Novice_Developer Aug 29 '16 at 10:30

1 Answers1

0

i must be declared global in every function that uses it:

function Update(block)

global i

% ...... 

There is also an error in line

block.OutputPort(1).Data(i) = block.InputPort(1).Data;

It is because block.InputPort(2).Data in setup is empty and block.OutputPort(1).Data has size 1. May be it is better to set the buffer size in s-block parameter dialog rather than in second input of the block?

AVK
  • 2,130
  • 3
  • 15
  • 25
  • That is why I was confused that why despite having a same script `i` have to be declared in each function , I used Dwork Vector instead and it works now ... Thankyou so much for the answer – Novice_Developer Aug 29 '16 at 10:31