Main Content

Create Standalone Executables with Variants Using Simulink Compiler

This example shows you how to create a standalone executable with a Variant Subsystem using the Simulink® Compiler™ workflow. You can use the same workflow for the Variant Source and Variant Sink blocks as well. Set the Variant activation time of the Variant block to startup. With 'startup' variant activation time, you can change the active variant via an input to the executable.

Simulink Compiler enables you to share Simulink simulations as standalone executables. You can build the executables by packaging the compiled Simulink model and the MATLAB® code used to set up, run, and analyze a simulation.

In this example you will:

1. Create and Deploy a Script with Simulink Compiler.

2. Write MATLAB app which can tune the variant control value externally.

Explore the Model

Open the model sldemo_variant_subsystems. The model contains a variant subsystem block Controller with two choices Linear Controller and Nonlinear Controller with the conditions VSS_MODE == 1 and VSS_MODE == 2 respectively.

Set the Variant activation time to startup in the Block Parameters dialog.

Create and Deploy a Script with Simulink Compiler

1. Write the script to deploy - Create a function called deployedScript. This code creates a Simulink.SimulationInput object for the model. variantControl is the value that we pass through the setVariable method for the tunable parameter VSS_MODE.

To configure the Simulink.SimulationInput object for deployment, use the function simulink.compiler.configureForDeployment.This sets the simulation mode to Rapid Accelerator.

  function deployedScript(variantControl)
   in = Simulink.SimulationInput('sldemo_variant_subsystems');
   in = in.setVariable('VSS_MODE',variantControl);
   in = simulink.compiler.configureForDeployment(in);
   out = sim(in);

2. Compile Script for Deployment - Before compiling the script that you want to deploy, ensure that the files for the model and script, in this case sldemo_variant_subsystems and the deployedScript.m, are included on the MATLAB search path.

To compile the script, use the mcc command with the script name. For more information, see mcc (MATLAB Compiler). This generates the stand alone application, deployedScript.exe, which you can execute outside of MATLAB or Simulink.

mcc -m deployedScript.m

3. Run the Deployed Script - To run the deployed executable, you need an appropriate runtime environment. To install the MATLAB Runtime, see You can run the deployed application only on the platform that the deployed application was developed on. Run the deployed application, deployedScript.exe, from the Windows command prompt.

To see the output for when the active variant choice is VSS_MODE = 1,

./run_deployedScript.exe 1

To see the output for when the active variant choice is VSS_MODE = 2,

./run_deployedScript.exe 2

Create the App in App Designer

You can write MATLAB app which can tune the variant control value externally. To learn more about how to create an app using the App Designer, see App Designer and Create and Run a Simple App Using App Designer.

1. Launch App Designer from the MATLAB command prompt.


2. Drag and drop a Edit Field and a push Button as shown below.

3. Write a callback function for the Simulate button.

Callback function: SimulateButton, VariantControlVSS_MODEEditField
function SimpleButtonPushed(app, event)
 in = Simulink.SimulationInput('sldemo_variant_subsystems');
 in = in.setVariable('VSS_MODE',app.VariantControlVSS_MODEEditField.Value);
 in = simulink.compiler.configureForDeployment(in);
 out = sim(in);
 ax = uiaxes;
 plot(ax, out.yout);

4. Launch the app to tune the value of the variant control. Simulate with different values to see the result.

See More