Development of a C MEX S-function for Integrating an Existing Simulation into Simulink

As part of a larger project, a component was deemed needed to link the Simulink® environment with existing simulation code written in C++. The incorporation of Simulink into the system was important (and highly desirable) because of the freedom and flexibility it offered in experimenting with algorithms used to control entities within the simulation. Experimenation to understand how to control the entities was a key part of the larger project.

An initial step in the process was to incorproate the existing C++ simulation environment into a higher-level C++ binary which, in addition, was to be linked to other another component via a virtual serial port connection. A large part of the exercise was integrating this second component which required management of the communication between the two components and the associated construction and interpretation of the message sentences sent and received. This stand-alone simulation program was then compiled as a Simulink MEX S-Function using a Microsoft Visual Studio® solution. Microsoft’s C++ compiler is one of the few supported to create MEX S-Functions.

The compiled S-Function was then used as a block within a Simulink model along with other Simulink model blocks. The S-Function took inputs from the Simulink model, ran the simulation for a period of time and then provided output from the end of that simulation period. This was then used by the rest of the Simulink model which calculated new input values that were then used as new input to the S-Function, thus,  completing a feedback loop. The part of the model concerning the control of entites within the simulation was then manipulated within the Simulink environment, which facilitated rapid experimentation with control schemes.

Development Summary


Revision Control: CVS
Language System: C++/Simulink
IDE: Visual Studio 2008
Build Tool: Visual Studio 2008
Compiler: Microsoft C++ Compiler

Comments are closed.
%d bloggers like this: