0
0
SimulinkHow-ToBeginner · 4 min read

How to Use S-Function in Simulink: Syntax, Example, and Tips

In Simulink, use S-Function blocks to create custom blocks by writing code in MATLAB, C, or other languages. You define the block behavior by implementing specific callback methods inside the S-Function file, then add the S-Function block to your model referencing that file.
📐

Syntax

The basic syntax to use an S-Function in Simulink is to create a MATLAB or C file that defines the block behavior, then add an S-Function block in your Simulink model referencing this file.

For MATLAB S-Functions, the function signature looks like:

function msfcn_name(block)

where block is the Simulink block object you configure inside the function.

For C S-Functions, you implement predefined callback functions like mdlInitializeSizes, mdlOutputs, etc.

matlab
function msfcn_example(block)
    setup(block);
end

function setup(block)
    % Register number of input and output ports
    block.NumInputPorts  = 1;
    block.NumOutputPorts = 1;

    % Setup port properties
    block.SetPreCompInpPortInfoToDynamic();
    block.SetPreCompOutPortInfoToDynamic();

    % Set block sample time
    block.SampleTimes = [0 0];

    % Register methods
    block.RegBlockMethod('Outputs', @Output);
end

function Output(block)
    block.OutputPort(1).Data = 2 * block.InputPort(1).Data;
end
💻

Example

This example shows a MATLAB S-Function that doubles the input signal. You create the function file msfcn_example.m with the code below, then add an S-Function block in Simulink and set its function name to msfcn_example.

When you run the model with an input signal, the output will be twice the input.

matlab
function msfcn_example(block)
    setup(block);
end

function setup(block)
    block.NumInputPorts  = 1;
    block.NumOutputPorts = 1;
    block.SetPreCompInpPortInfoToDynamic();
    block.SetPreCompOutPortInfoToDynamic();
    block.SampleTimes = [0 0];
    block.RegBlockMethod('Outputs', @Output);
end

function Output(block)
    block.OutputPort(1).Data = 2 * block.InputPort(1).Data;
end
Output
If input = 3, output = 6
⚠️

Common Pitfalls

  • Not registering all required methods: Forgetting to register methods like Outputs causes the block to do nothing.
  • Incorrect port setup: Mismatched input/output port numbers or sizes cause errors.
  • Sample time mismatch: Not setting sample times properly can lead to simulation errors.
  • File naming: The S-Function file name must match the function name used in the block.
matlab
%% Wrong: Missing Outputs method registration
function msfcn_wrong(block)
    block.NumInputPorts  = 1;
    block.NumOutputPorts = 1;
    block.SetPreCompInpPortInfoToDynamic();
    block.SetPreCompOutPortInfoToDynamic();
    block.SampleTimes = [0 0];
    % Missing: block.RegBlockMethod('Outputs', @Output);
end

function Output(block)
    block.OutputPort(1).Data = block.InputPort(1).Data;
end

%% Right: Register Outputs method
function msfcn_right(block)
    block.NumInputPorts  = 1;
    block.NumOutputPorts = 1;
    block.SetPreCompInpPortInfoToDynamic();
    block.SetPreCompOutPortInfoToDynamic();
    block.SampleTimes = [0 0];
    block.RegBlockMethod('Outputs', @Output);
end

function Output(block)
    block.OutputPort(1).Data = block.InputPort(1).Data;
end
📊

Quick Reference

  • Define S-Function file: Create MATLAB or C file with required callbacks.
  • Add S-Function block: In Simulink, drag S-Function block and set function name.
  • Register methods: Use RegBlockMethod to define behavior like Outputs.
  • Set ports and sample time: Configure input/output ports and sample times properly.
  • Test with simple input: Verify output matches expected behavior.

Key Takeaways

Create an S-Function file defining block behavior using MATLAB or C code.
Add an S-Function block in Simulink and set its function name to your file's function.
Register necessary methods like Outputs to define how the block processes data.
Configure input/output ports and sample times correctly to avoid simulation errors.
Test your S-Function block with simple inputs to ensure it works as expected.