Guy on Simulink

Simulink & Model-Based Design

Getting Started with Simulink Compiler

In MATLAB R2020a, we introduced a new product: Simulink Compiler

In this post, I am sharing the basic information you will need to get started with Simulink Compiler.

What is Simulink compiler?

To understand how Simulink Compiler works, it's a good idea to begin by getting familiar with MATLAB Compiler. With MATLAB Compiler, you can take pretty much any MATLAB code and turn it into a standalone application that your end-users will be able to run royalty-free using MATLAB Runtime.

Depending on your needs, the final standalone application can take various forms and be shared in multiple ways. Here are a few examples:

  • Standalone executable: Give your end user an executable that they can run on their machine without a MATLAB installation.
  • Web Apps: Create a MATLAB App using App Designer and package it as a web app. Using the MATLAB Web App Server, you will be able to host it and simply share a hyperlink to your end users who will use the compiled app in a web browser.
  • Software components: Use MATLAB Compiler SDK to package a MATLAB program into software components to be integrated with other programming languages like C/C++ shared libraries, Microsoft® .NET assemblies, Java® classes, and Python® packages. Large-scale deployment to enterprise systems is supported through MATLAB Production Server.
  • Microsoft Excel Add-ins: Give Excel users in your company the power of MATLAB and Simulink through Excel.
  • Take a look at the product pages for MATLAB Compiler and Simulink Compiler for more details on all the capabilities.

    If you ask me to summarize in a few words the new capabilities that Simulink Compiler adds on top of MATLAB Compiler, my answer would be that with Simulink Compiler the deployed MATLAB code can now use the sim command.

    A Simple Example

    When describing new features, I like to go through a simple example. For this case, let's get started with a simple mass-spring-damper, similar to the starting point of the Basic Modeling Workflow example we added to the Simulink documentation a few releases ago:

    Simple test model

    For convenience, I defined default values for the parameters and input signal in the model workspace (This is not necessary, but I find it useful to have default values in the Model Workspace. That way I can test the model as I am developing it without having any variables defined in the base workspace):

    Model workspace

    I can simulate the model with a simple function like this one:

    MATLAB Function simulating the model

    If you would like to deploy this function using Simulink Compiler, you only need to add one line of code to configure the SimulationInput object for deployment.

    Configuring for deployment

    What simulink.compiler.configureForDeployment does can be summarized as basically one thing: it sets the simulation mode to Rapid Accelerator. You can read more on this requirement and its implications here.

    Finally, you generate the executable using the mcc function.

    Compiling the executable

    Once the executable is ready, you can launch it from a DOS prompt or by double-clicking on it in Windows Explorer.

    Launching the executable

    Tuning Parameters and Input signals

    The executable we just created only simulates the model with the same input signals and the same parameter values as used when compiling it. For more realistic applications, you probably want the end-user to be able to specify new values and observe the impact on the simulation results.

    This can be done in many ways; here is an example where I made 3 block parameters input arguments to the function and applied them to the simulation using the setVariable method of the SimulationInput object.

    Tuning Parameters

    Once deployed, I can then pass new values to the executable:

    Launching the executable

    Here is a different example where I read a new input signal from CSV file and specify it using setExternalInput.

    Tuning Input Signal

    As you can see, tuning parameters and input signals is not more complicated for a deployed application then it is for code running in a MATLAB installation.

    Deploying an App with App Designer

    Now that we have seen the basics of configuring a SimulationInput object for deployment and tuning parameters and input signals, let's move to something more interesting: Deploying a MATLAB App simulating a Simulink model.

    To get started, I launched appdesigner and created a simple app with 3 sliders, one push button and one axes:

    Simple app

    I created a Button Pushed Callback and added the following code, which is very similar to one of the above example:

    Pushed button callback

    With that, we are ready to package the app as an executable.

    Packaging an executable

    Now we have an app that can be run outside of MATLAB.

    the executable

    If you prefer, you can also deploy your app as a Web App.

    Packaging a Web app

    That way, once you have a Web App Server running, your end-users will be able to run the app from a web browser.

    Using a Web app in a web browser

    What's Next?

    In this blog post, we went through the basics of Simulink Compiler. I recommend going through the documentation example for Deploying an App Designer Simulation with Simulink Compiler. This will teach you a few more tricks to improve the usability of your app.

    In my next post, I will describe how I built an app for the COVID-19 exponential spread simulator I published a few months ago. This will allow me to cover more advanced tips and tricks for deploying more complex apps with Simulink Compiler.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.