Main Content

Create and Deploy a Script with Simulink Compiler

In this example, you prepare a model to work with Simulink® Compiler™, develop and compile the script, and then deploy it as a standalone application.

Prepare the Model

Simulink Compiler uses rapid accelerator simulation targets to generate an executable to submit a Simulink model. Simulink Compiler only supports models which can run in rapid accelerator mode. To set the simulation mode of the model to rapid accelerator, use the model parameter 'SimulationMode' with SimulationInput object. To enable simulation deployment of the model, your model must be supported by the Rapid Accelerator mode correctly.

Simulink Compiler only supports sim function syntax that takes Simulink.SimulationInput object and returns Simulink.SimulationOutput object.

If callbacks are present in the model, they are called during the build time of the application. However, once the application or the script is deployed, these callbacks are not invoked.

Write the Script to Deploy

After preparing the model, write the script that you would like to deploy. In this example, we use a model and change one of the tunable parameters in the script.

In the MATLAB® Editor, create a function deployedScript. This example uses the model sldemo_suspn_3dof. In this function, create a Simulink.SimulationInput object for the model, sldemo_suspn_3dof and change the value of Mb with the setVariable method of the Simulink.SimulationInput object. To ensure that the model runs in rapid accelerator mode, set the SimulationMode to Rapid through the setModelParameter method of the Simulink.SimulationInput object or use the simulink.compiler.configureForDeployment function as shown below.

The variables modified in the simulations can be in the base workspace or in the top model workspace. If your model uses external input variables, then, you can not use the method in.setExternalInput of the Simulink.SimulationInput. External input variables must be in the MATLAB workspace before packaging for deployment.

function deployedScript()
    in = Simulink.SimulationInput('sldemo_suspn_3dof');
    in = in.setVariable('Mb', 1000);
    in = simulink.compiler.configureForDeployment(in);
    out = sim(in);
end

Save the function as a deployedScript.m.

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_suspn_3dof and the deployedScript.m, are included on the MATLAB search path. To compile the script, use the mcc command with the script name. To learn more about the mcc command, see mcc.

mcc -m deployedScript.m

Troubleshooting Tips

Simulink Compiler automatically packages the dependencies in the model and the deployed scripts. If the command mcc cannot find a dependency, you might see errors.

  • If you see the error "Unable to resolve the name Simulink.SimulationInput", check that the model is on the path.

  • If the dependent files are located in another directory, attach them by using the flag -a. For example, mcc -m scriptName.m -a myDataFile.dat.

Run the Deployed Script

Install MATLAB Runtime

To run the deployed executable, you need an appropriate runtime environment. To install the MATLAB Runtime, see https://www.mathworks.com/products/compiler/matlab-runtime.html.

Run the Deployed Application

You can run the deployed application only on the platform that the deployed application was developed on.

Run the deployed application from the Windows® command prompt. Running the deployed application from the command prompt enables the application to print diagnostic messages in the command prompt when it encounters errors. These messages can be a helpful tool in troubleshooting the problem.

See Also

| | | |

Related Topics