Guy on Simulink

Simulink & Model-Based Design

Commonly Asked Questions About S-Functions

Today, we are welcoming Michael Carone, who provides responses to the most frequent questions we receive about S-Functions.

What can you use S-Functions for?

S-Functions allow you to create custom blocks. They can be useful to do things like:

  • Bring legacy code into a model
  • Create a custom user interface
  • Interface with a 3rd party tool

Do I have to use C to write an S-Function?

No. S-Functions can be written in C, C++, MATLAB, and FORTRAN.

If your code is written in other languages that can be called from MATLAB or C/C++, you can create a S-Function wrapper to include it in a Simulink model.

How do S-Functions compare with MATLAB Functions, MATLAB System blocks, and other blocks for creating user-defined functions?

I strongly recommend reading our documentation page Comparison of Custom Block Functionality. Depending on your needs the following flowchart can help you determine which option is best for you.

Block Type Flowchart

When I update to a new version of Simulink, does the S-Function need to be rewritten or recompiled?

No need to rewrite the S-Function. S-Functions are backward compatible in terms of their source code. A C-MEX S-Function written in an older release that is recompiled in a newer release retains the functionality and behavior from the older release.

In the majority of cases, you do not need to recompile the S-Function. S-Function MEX files compiled in older releases can work in a new release if the platform and associated libraries either remain unchanged or maintain backward compatibility. For best results, we recommend that you recompile the S-Function source code in your current version of MATLAB.

What about moving from 32-bit to 64-bit, or from one operating system to another?

S-Functions need to be recompiled when moving from 32-bits to 64-bits, and from one operating system to another.

Do I have to create S-Functions from scratch?

There are two tools available to help you create S-Functions: The Legacy Code Tool and the S-Function Builder.

The philosophy of those two tools is similar. You start with some C code implementing your algorithm. You specify the properties of this code, for example number of inputs, outputs, their dimensions and data types, and external dependencies on which your code depend on. The tools then automatically generate a wrapper S-Function and compile it so you can use it in Simulink.

The main difference between those two tools is that the S-Function Builder is a graphical interface where you interactively specify the properties of your code.

S-Function Builder

The Legacy Code Tool is a function allowing you to programmatically specify the properties of your code.

Legacy Code Tool

Now it's your turn

Let us know if you have good guidelines and tips on how, when, and why to use S-Functions by leaving a comment here.

|
  • print

コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。