Main Content


Set code for methods of S-function generated using S-Function Builder block


    Simulink.SFunctionBuilder.setUserCode(blk,methodType,userCode) specifies the code userCode as part of the S-function for the S-Function Builder block blk. Use this function to specify include statements, global variables, and code for the S-function methods.

    Input Arguments

    collapse all

    S-Function Builder block, specified as a block handle or as a string or a character vector that defines the block path.


    • To get the block handle for a block in your model, use the getSimulinkBlockHandle function.

    • To get the block handle for the most recently selected or added block, use the gcbh function.

    S-function method for which to specify code, specified as one of these options:

    • 'include' — Include statement

    • 'externs' — Global variable declaration

    • 'start' — Start method

      Use the start method to allocate memory at the start of the simulation. Reference the allocated memory throughout the S-function code using PWorks.

    • 'output' — Output method

      The output method computes the block output values for each simulation time step.

    • 'update' — Update method

      The update method computes the value of discrete states for the next simulation time step. Only define the update method when the S-function has discrete states.

    • 'derivatives' — Derivatives method

      The derivatives method computes the value of state derivatives. Only define the derivatives method when the S-function has continuous states.

    • 'terminate' — Terminate method

      Use the terminate method to deallocate memory at the end of simulation.

    Example: Simulink.SFunctionBuilder.setUserCode(blk,"output","*y0 = "myCFunction(*u0);")


    Data Types: char | string

    Code for S-function method, specified as a string or a character vector that defines C or C++ code for part of the S-function.

    Example: Simulink.SFunctionBuilder.setUserCode(blk,"output","*y0 = "myCFunction(*u0);")


    To specify the target language for the S-function, use the Simulink.SFunctionBuilder.setTargetLanguage function.

    Data Types: char | string

    Version History

    Introduced in R2022a