Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Convert a MEX-function to an S-function

Asked by Martijn on 29 May 2012

Dear all,

I want to implement an opensource nonlinear solver in Simulink such that it is suitable for code-generation (XPC-target). For this I want to convert an existing .mex file into an S-function.

Some background info:

I am using the IPOPT nonlinear solver which is supplied with the OptiToolbox (opensource) compliled as a precompiled .mex file which works perfectly under Matlab. However, to be able to use the solver in XPC-target I need to create and S-function block in Simulink.

Other options I've tried so far:

  • Since I already have a mex-file I thought it would be convenient to use that inside a simple 'Matlab Function'-block. This works in Simulink, but the code generator cannot compile the Simulink diagram to a real-time executable code.
  • Creating an S-function in which I call the .mex function. This code cannot be compiled since the .tlc file is missing.


After some search on fora and Matlab central, I found out that I should create a C MEX S-function and compile it. To make life easier, Simulink comes with an S-function builder in which libraries, headers etc. can be added. I failed in constructing one and it is driving me crazy.

I think it would be easiest to convert the .mex function into an S-function (if possible). My question to you therefor is:

Who has experience in converting an .mex function (based on a large set of C++ routines) into an S-function in Simulink

Your help will be greatly appreciated!



1 Answer

Answer by TAB on 30 May 2012
Accepted answer

It is not at all possible to convert a .mex file into S-function.

Let me clarify something...

  • S-Function is a C/C++ (also Ada) style routines used in simulink to construct custom blocks with our own logic.
  • When source code of S-function is built, it generated .mex files which can executed by simulink in the simulation.
  • The comiled .mex files is for simulation purpose only and it can not support code generation. If you have S-function source, code can generated from it, but it used matlab's SimStruct library routines which are heavy and not feasible to use with embedded target.
  • For proper code generation each s-function should be supported by its in-lining TLC file which is used by RTW to generate code for that particular s-function block.

Now for your issue, you can try this...

If you have C/C++ routines as source (I mean source code of your mex file), no need to convert them all to S-function routine. Simply create a simulink S-function and call your existing C/C++ function in this s-function. You can do this by using S-function builder or legacy code tool. These both tools generates S-function and their inlining TLC files. Once generated, files can be modified as per need.


Example Using S-Functions to Incorporate Legacy C Code

Integrating Existing C Functions into Simulink Models with the Legacy Code Tool

Legacy Code Tool



Contact us