| Contents | Index |
| On this page… |
|---|
About Code Generation from Simscape Models Related Simulink Code Generation Documentation |
You can use Simulink Coder™ software to generate stand-alone C or C++ code from your Physical Networks models and enhance simulation speed and portability. Certain features of Simulink software also make use of generated or external code. This section explains code-related tasks you can perform with your Simscape models.
Code versions of Simscape models typically require fixed-step Simulink solvers, which are discussed in the Simulink documentation. Some features of Simscape software are restricted when you translate a model into code. See How Simscape Code Generation Differs from Simulink, as well as Limitations.
Note Code generated from Simscape models is intended for rapid prototyping and hardware-in-the-loop applications. It is not intended for use as production code in embedded controller applications. Add-on products based on the Simscape platform also support code generation, with some variations and exceptions described in their respective documentation. |
Consult Simulink Acceleration modes, Simulink Coder, and xPC Target™ documentation for general information on code generation.
Code generation has many purposes and methods. There are two essential rationales:
Compiled code versions of Simulink and Simscape models run faster than the original block diagram models. The time savings can be dramatic.
An equally important consideration for Simscape models is the stand-alone implementation of generated and compiled code. Once you convert part or all of your model to code, you can deploy the stand-alone executable program on virtually any platform, independently of MATLAB.
Converting a model or subsystem to code also hides the original model or subsystem.
With Simulink, Simulink Coder, and xPC Target software, using several code-related technologies, you can link existing code to your models and generate code versions of your models.
| Code-Related Task | Component or Feature |
|---|---|
| Link existing code written in C or other supported languages to Simulink models | Simulink S-functions to generate customized blocks |
| Speed up Simulink simulations | Accelerator mode Rapid Accelerator mode |
| Generate stand-alone fixed-step code from Simulink models | Simulink Coder software |
| Generate stand-alone variable-step code from Simulink models | Simulink Coder Rapid Simulation Target (RSim) |
| Convert Simulink model to code and compile and run it on a target PC | Simulink Coder and xPC Target software |
In general, using the code generated from Simscape models is similar to using code generated from regular Simulink models. However, there are certain differences.
Simulink Coder software generates code from the Simscape blocks separately from the Simulink blocks in your model. The generated Simscape code does not pass through model.rtw or the Target Language Compiler. All the code generated from a single model resides in the same directory, however.
To generate and execute Simscape code, you must have a compiler and a processor that support:
64-bit precision floating-point arithmetic
32-bit integer size
Although the lcc compiler, which is installed along with MATLAB, is supported, for complex models the generated C-code complexity can exceed the capacity of lcc. MathWorks recommends that you consider using a commercial compiler. For example, Microsoft® Visual Studio® Express is available at no charge and compatible with Simscape software. For details on supported compiler versions, see
http://www.mathworks.com/support/compilers/current_release
Simscape software and its add-on products provide static runtime libraries precompiled for compilers supported by Simulink Coder software. These are the standard UNIX compilers for UNIX operating systems, lcc and Microsoft Visual Studio for 32-bit Windows®, and Microsoft Visual Studio for 64-bit Windows.
For all other compilers, the static runtime libraries needed by code generated from Simscape models are compiled once per model during the code generation build process.
Reusable subsystems in Simulink reuse code that is generated once from the subsystem. You cannot generate reusable code from subsystems containing Simscape blocks.
A tunable parameter is a Simulink run-time parameter that you can change while the simulation is running. Simscape blocks do not support tunable parameters in either simulations or generated code.
If you choose to enable parameter inlining for code generated from a Simscape model, the software inlines all its run-time parameters. If you choose to make some of the global Simscape block parameters exceptions to inlining, the exceptions are ignored. You can change global tunable parameters only by regenerating code from the model.
![]() | Troubleshooting Simulation Errors | Real-Time Simulation | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |