If this is all you want your S-function to do, then you can simply implement your own subsystem (using a unit delay block to store the counter's state) or use an Embedded MATLAB Fcn block containing code to the effect of:
function y = fcn(u)
persistent counter;
if isempty(counter)
counter = 0;
end
if (u == 1)
counter = counter + 1;
end
y = counter;
Is there a reason you chose to use a C-MEX S-function instead? (By the way, I would recommend Level-2 MATLAB file S-functions or Embedded MATLAB Functions if you are not comfortable with C/C++ and need to perform simple enough operations)
Best Answer