Main Content

Generate, Modify, and Deploy a MATLAB App for a Simulink Model

You can use the simulink.compiler.genapp function to automatically generate a MATLAB® app for a Simulink® model. You can compile and deploy the generated app using the mcc command. This example shows you how to generate an app for a model, compile and deploy the app, and customize the app using the App Designer.

The MultiSimPaneApp template is the default template for app generation. With this template, the generated app is deployable and customizable. This template also allows you to pause a simulation, tune parameters in the middle of a simulation, and configure an app for deployment.

Generate an App with MultiPaneSimApp Template

This example shows you how to use the simulink.compiler.genapp function to generate a deployable app for the model f14_mod. You can use the generated app to tune the parameters of the model and run simulations with different parameter values. You can also customize the app in the App Designer and deploy the generated app for use outside of MATLAB®.

Open the Model

Open the model f14_mod.

Generate MATLAB App for Model

To generate the app, the SaveFormat parameter for the output data must be Dataset or Structure with time. To change the SaveFormat parameter for the f14_mod model, in the Modeling tab of the Simulink Toolstrip, go to Model Settings. In the Data Import/Export pane, change Format to Dataset.

You can generate a MATLAB app for the model from the Simulink Toolstrip. On the Simulink Toolstrip, under Save, select Export Model to Simulation App. Use the default template MultiPaneSimApp to generate the app. Enter the app name as f14app and browse to choose a destination folder. Then, to create the app, click Create.

Alternatively, you can use the simulink.compiler.genapp function to generate an app for the f14_mod model. Running the simulink.compiler.genapp function with the model name as an argument generates an app named f14app, which is specified as an argument. The default name of the generated app is modelName_SLSimApp. Simulink Compiler™ uses a default template to generate the app. You can tune the parameters and simulate the model for which the app is generated. The generated app also provides a plot of the simulation results. All the related app files are generated into the f14app directory.

simulink.compiler.genapp('f14_mod','AppName','f14app');

After generating the app, click to simulate the app.

On the right side of the model image, you can tune the parameter values and run simulations with different values.

Plot Layouts

Use the up arrow at the bottom of the app window to display the plot for the simulation output. The Plot Layout controls allow you to add multiple plots. By default, this pane displays six plots. You can add or remove axes by using Plot Layout controls on the bottom left of the app.

Use the Scopes drop-down menu to select a plot, and use the check boxes to select logged signals to display. This functionality allows you to compare results and modify the app as needed.

The default maximum number of scopes in the generated app is nine plots, which are displayed in a 3x3 grid. If the number of scopes in the model is greater than the maximum allowed scope count in the generated app, the software increases the maximum number of allowed scopes to be equal to the closest perfect square greater than the current scope count. When using the Plot Layout controls to decrease the number of scopes, followed by an increase, any model scopes that were removed are recreated.

The generated app lists all the automatically generated scopes in the Scopes drop-down menu that appears in the Signals pane at the bottom left of the split view. As you add or remove scopes using the Plot Layout controls, the Scopes drop-down menu updates, adding or removing scope names to or from the list. When inports are present in the model, the Input Signals section of the Signals pane is automatically populated with check boxes representing these signals.

Input Signals

Signals listed under the Input Signals section are obtained from one of these sources:

  • Pre-app generation: Input signals that are present in Model Configuration Parameters > Data Import/Export > Load from workspace > Input.

  • Post-app generation: Input signals loaded using the File > Load > Load Parameters and Inputs menu in the generated app, or input signals saved as a part of a previous session.

Files and Folders

Once the app is generated, three folders are created in the app folder:

  • AppHelper — Contains the code that the generated app uses. You can use this folder to navigate through the app, to understand how the app works, and customize the app.

  • Assets folder — Contains all the assets the app uses, such as images, styles, HTML, JavaScript, model data, and some metadata.

  • AppUtils — Contains scripts to help you deploy the app as a standalone desktop app or a web app, test the deployed standalone app in MATLAB, and run the generated app directly from MATLAB.

See Also

| | | |

Related Topics