MATLAB: How to create a sawtooth signal inside an S-function

s-functionsawtoothsimulink

Hi,
I'm working with C-MEX S-function (C code). I was wondering if it is possible to create a sawtooth signal inside a C-coded S-function and see the signal with the Simulink scope. If it is possible, how to do it ?
Thanks for your help

Best Answer

A simple demonstration:
  1. Drag a S-function builder block in the model.
  2. Configure S-function having only 1 output (y[0]). No input, No discrete state, No continuous state.
  3. In the output function write code given below
  4. Build the S-function and run the model.
y0[0] = y0[0]+1;
if(y0[0]>UPPER_LIMIT)
{
y0[0] = 0;
}