Dummy output S-function for Simulink

This minimal S-function implements a dummy output function. It receives a double input signal and ignores it completely. It does not perform any further action.

This S-function can be used to prevent Simulink from optimizing away blocks that have no effect on the model output. For example, if you have a block that performs some calculations but its output is not used anywhere, Simulink might optimize it away during code generation. By connecting the output of such a block to this dummy output S-function, you can ensure that the block remains in the model since Simulink can’t tell whether the output is used or not.

dummy_output.cpp

#define S_FUNCTION_NAME  dummy_output
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"
#include <cstdio>  // For file handling

static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, 0);  // No parameters
    ssSetNumContStates(S, 0);  // No continuous states
    ssSetNumDiscStates(S, 0);  // No discrete states
    // 1 input port with 1 element
    if (!ssSetNumInputPorts(S, 1)) return;
    ssSetInputPortWidth(S, 0, 1);  // Input port width = 1
    ssSetInputPortDataType(S, 0, SS_DOUBLE);
    ssSetInputPortComplexSignal(S, 0, COMPLEX_NO);

    ssSetInputPortRequiredContiguous(S, 0, 1);  // Require contiguous input port memory
    // ssSetInputPortDirectFeedThrough(S, 0, 1);  // Direct feedthrough

    // No output ports
    if (!ssSetNumOutputPorts(S, 0)) return;
    // Sample times
    ssSetNumSampleTimes(S, 1);  // Single sample time

    // Work vectors
    ssSetNumRWork(S, 0);  // Real work vector
    ssSetNumIWork(S, 0);  // Integer work vector
    ssSetNumPWork(S, 0);  // Pointer work vector for file pointer
    ssSetNumModes(S, 0);  // Mode vector
    ssSetNumNonsampledZCs(S, 0);  // Zero crossings

    // Make the S-function block capable of being used in a Real-Time Workshop-generated model
    ssSetOptions(S, 0);
}

#define MDL_START
static void mdlStart(SimStruct *S) {
}

// Function: mdlInitializeSampleTimes =========================================
// Abstract:
//    Initialize the sample times to be 1ms (1kHz)
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetInputPortSampleTime(S, 0, INHERITED_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
}

// Not needed for this example
static void mdlOutputs(SimStruct *S, int_T tid) {
    // Do nothing
}

#define MDL_UPDATE
static void mdlUpdate(SimStruct *S, int_T tid)
{
    // Ignore value - this is just a dummy output
}

static void mdlTerminate(SimStruct *S)
{
}

// Required S-function trailer
#ifdef MATLAB_MEX_FILE
#include "simulink.c"   // MEX-file interface mechanism
#else
#include "cg_sfun.h"    // Code generation interface
#endif

How to compile

mex dummy_output.cpp