deploy an app designer simulation with simulink compiler
this example walks you through the workflow of creating a simulation app in app designer and using simulink® compiler™ to deploy it. the example explains the code that is used to build the app.
to open the example, type the following in the matlab® command window, or click the view matlab code button.
openexample('simulinkcompiler/deployingasimulationappusingsimulinkcompilerexample')
deploying a simulation app with simulink compiler
in this example, we use an app that is prepared in the app designer and deploy it with simulink® compiler™.
open and explore model
in this example, we use the model of a mass springer damper system. the mass-spring-damper model consists of discrete mass nodes distributed throughout an object and interconnected via a network of springs and dampers. this model is well-suited for modelling object with complex material properties such as non-linearity and elasticity. in this example we use the mass spring damper system. the system is parametrized by mass (m), spring stiffness (k), damping (b) and the initial position (x0). the input to the system is the applied force.
to explore this model with different values of the tunable parameters, create the following model workspace variables:
mass - m.
spring stiffness - k.
damping - b.
initial position - x0.
open_system('massspringdampermodel');
create the app in app designer
use the matlab app designer to create an app to simulate the model with different parameter values and input signals. to learn more about how to create an app using the app designer, see use the massspringdamperapp.mlapp
file to use the app.
massspringdamperapp
app details
the main part for the app is the simulate button callback function. it has the following salient parts: setup the simulationinput
object, configure it for deployment, simulate, and plot the simulation results.
the functionality of the application to change and experiment with the tunable parameters is defined in the callback function simulatebuttonpushed
. this callback function enables you to change,experiment and analyze different simulations by modifying the values in the app designer.
callback functions
this section explains the code written to create the app, massspringdamperapp
. the code for the massspringdamperapp
uses many callback functions. you can inspect the code by opening the massspringdamperapp,mlapp
file in the app designer. the createsimulationinput
and modifyparameterduringsim
functions help set the values while also updating the simulation values in the app. we use the simulink.simulationinput
object to set the variables to the model and use these variables to change the values and analyze the model.
create the simulink.simulationinput
object in the createsimulationinput
function
in the simulatebuttonpushed
function, create a simulationinput
object, siminp for the model massspringdampermodel
. use the setvariable
method on the simulationinput
object to load the parameter values into the variables k
, m
, b
, and x0
. now that we have assigned all the values to the variables and set the input signal, the simulink.simulationinput
object is required to be configured for deployment. use the simulink.compiler.configurefordeployment
function of simulink compiler. this function handles all the settings required for the script to be compatible for deployment by setting the simulation mode to rapid accelerator, and by setting the parameter rapidacceleratoruptodatecheck
to off
.
modify parameters during simulation
to allow modification of the parameters during the simulation, use the function |modifyparameterdurinsim|. this function gets the current simulation stats and then if the simulation is running or paused, enables you to modify the values for the simulation.
simulate and plot the results
use the configured simulink.simulationinput object to run the simulation with the designed app. the addpoints
function plots and traces the simulation results as the app is running.
test out the application in app designer
before deploying the application, ensure that the app runs in the app designer. click the simulate button on the app to verify that the application works by simulating the model for different values.
compile script for deployment
to compile the app, use the mcc
command, followed by the script name.
mcc -m massspringdamperapp.mlapp
external websites
running the deployed application
install matlab runtime and package the deployable
to run the deployed executable, you need an appropriate runtime environment. for more information, see matlab runtime.
ensure that the path environment variable is free of other instances of matlab runtime from previous installs. if there are any, remove them.
to install matlab runtime, follow the instructions on install and configure matlab runtime.
compile the deployable for the first time as follows:
enter
deploytool
command in the matlab command window and select application compiler.in the main file section, add the file to be deployed,
massspringdamperapp.mlapp
in the packaging options section on the toolstrip, select runtime included in package and enter the
deployed_installer
in the text box.click package in the package section of the toolstrip.
once the package is ready, use the
deployed_installer
in thefor_redistribution
folder to install the proper runtime environment for running the deployed application.
run the deployed application
you can run the deployed script only on the platform that the deployed script was developed on.
it is recommended to run the deployed application from the windows command prompt. running the deployed application from the command prompt also enables the script to print errors when something is wrong in the deployed application. these errors can help troubleshoot the problem.
note
the massspringdamperapp.mlapp
contains
errordlg
, and errordlg
is not supported on web
apps.
see also
| | |