You are on page 1of 17

S-Functions in Simulink

USING MATLAB AND S-FUNCTION BUILDER


S-Functions
•Stands for “System Functions”
•Custom-built Simulink blocks using C, C++, Fortran, or Matlab.
•Makes use of a special S-function API

•Involve three main concepts: Inputs, Outputs, and Action


Why do we need them?
•To simplify complex Simulink models
•To handle external hardware, programs, or files
•To cut calculation time
•Event-specific reactions in code (excluding matlab)
Simulink – Gain Block

Gain: Take input, multiply by value to produce output.


Matlab Example – Gain Block
•Replace gain block with custom s-function
•One input
•Multiply by 2
•One output
Matlab Example – Gain Block
•Check out the code now: s_doubler.m
Function Builder Example – Gain Block
Simulink – Product Block

Product: Takes numerous inputs and multiplies them together


Matlab Example – Product Block
•Simply replace product block with your
Matlab S-Function Block
•2 Inputs
• Amount defined by internal code

•Operates exactly the same


Matlab Example – Product Block
•Check out the code now: s_multiplier.m
Function Builder Example –
Product Block
Matlab Example – Throttle Limiter
•Input: Wave from -100 to 100, with noise
•Desired output:
• Scale between -1 and 1, even with noise
Matlab Example – Throttle Limiter
Simulink Example –
Throttle Limiter
•Built after Matlab S-Function
•Works entirely the same
Function Builder Example –
Throttle Limiter
Creating an S-Function - Matlab
•Load up the template.m file

•Let’s walk through the throttle limiter


Creating an S-Function – Function Builder
•Load up the fb_limiter.mdl

•Let’s walk through the steps

You might also like