0
0
SimulinkHow-ToBeginner · 4 min read

How to Write Level 2 S-Function in Simulink: Syntax and Example

To write a Level 2 S-Function in Simulink, create a MATLAB class that inherits from matlab.System. Implement key callback methods like setupImpl, stepImpl, and resetImpl to define block behavior. Then, register the S-function in Simulink using the S-Function block with your MATLAB file.
📐

Syntax

A Level 2 S-Function in Simulink is written as a MATLAB class that defines specific callback methods. The main parts include:

  • setupImpl: Initialize states and parameters.
  • stepImpl: Define outputs and update states each simulation step.
  • resetImpl: Reset states when simulation restarts.
  • getNumInputsImpl/getNumOutputsImpl: Specify number of inputs and outputs.

You save this class in a .m file and link it to a Simulink S-Function block.

matlab
classdef myLevel2SFunction < matlab.System
    methods (Access = protected)
        function setupImpl(obj)
            % Initialization code here
        end

        function y = stepImpl(obj, u)
            % Output calculation here
            y = u; % Example: output equals input
        end

        function resetImpl(obj)
            % Reset states here
        end

        function num = getNumInputsImpl(obj)
            num = 1; % One input
        end

        function num = getNumOutputsImpl(obj)
            num = 1; % One output
        end
    end
end
💻

Example

This example shows a Level 2 S-Function that passes input to output unchanged. It demonstrates the required methods and how to use the S-Function block in Simulink.

matlab
classdef passthroughSFunction < matlab.System
    methods (Access = protected)
        function setupImpl(~)
            % No initialization needed
        end

        function y = stepImpl(~, u)
            y = u; % Output equals input
        end

        function resetImpl(~)
            % No states to reset
        end

        function num = getNumInputsImpl(~)
            num = 1;
        end

        function num = getNumOutputsImpl(~)
            num = 1;
        end
    end
end
⚠️

Common Pitfalls

  • Forgetting to inherit from matlab.System causes errors.
  • Not implementing required methods like stepImpl or setupImpl leads to incomplete behavior.
  • Incorrect number of inputs or outputs causes Simulink connection errors.
  • Not saving the class file with the same name as the class causes loading failures.
  • Using Level 1 S-Function syntax instead of Level 2 results in incompatibility.
matlab
classdef wrongSFunction
    % Missing inheritance from matlab.System
    methods
        function y = stepImpl(~, u)
            y = u;
        end
    end
end

% Correct way:
classdef correctSFunction < matlab.System
    methods (Access = protected)
        function y = stepImpl(~, u)
            y = u;
        end
    end
end
📊

Quick Reference

MethodPurposeNotes
setupImplInitialize block states and parametersCalled once at simulation start
stepImplCalculate outputs and update statesCalled every simulation step
resetImplReset states on simulation resetOptional if no states
getNumInputsImplDefine number of inputsReturn integer
getNumOutputsImplDefine number of outputsReturn integer

Key Takeaways

Write Level 2 S-Functions as MATLAB classes inheriting from matlab.System.
Implement setupImpl, stepImpl, and getNumInputsImpl/getNumOutputsImpl methods.
Save the class file with the same name as the class for Simulink to load it.
Use the S-Function block in Simulink to call your Level 2 S-Function MATLAB file.
Avoid mixing Level 1 and Level 2 S-Function syntax to prevent errors.