| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simscape |
| Contents | Index |
| Learn more about Simscape |
| On this page… |
|---|
About Code Generation from Simscape Models Related Simulink Code Generation Documentation |
You can use Real-Time Workshop® 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 following in this section, 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. Vertical products based on the Simscape platform also support code generation, with some variations and exceptions described in their respective User's Guides. Consult those User's Guides for more information. |
Consult Simulink Acceleration modes, Real-Time Workshop, 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 application.
Converting a model or subsystem to code also hides the original model or subsystem.
With Simulink, Real-Time Workshop, 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 | Real-Time Workshop software |
| Generate stand-alone variable-step code from Simulink models | Real-Time Workshop Rapid Simulation Target (RSim) |
| Convert Simulink model to code and compile and run it on a target PC | Real-Time Workshop 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.
Real-Time Workshop 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.
Simscape software and its vertical products provide static runtime libraries precompiled for compilers supported by Real-Time Workshop 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.
![]() | Linearizing Simscape Models | Limitations | ![]() |

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