Deploy Generated MATLAB Functions from Symbolic Expressions with MATLAB Compiler
This example shows how to generate a MATLAB® function from a symbolic expression and use the function to create a standalone application with MATLAB Compiler™. The target system to run the application only requires a MATLAB Runtime installation and does not require a licensed copy of MATLAB.
This example follows the steps described in Create Standalone Application from MATLAB (MATLAB Compiler) and updates the steps to generate a MATLAB function from a symbolic expression.
Generate Deployable Function from Symbolic Expression
First, create a system of differential equations with state variables x(t)
and y(t)
, and input parameters alpha
and beta
.
syms x(t) y(t) alpha beta eqs = [x - diff(x,t) == alpha*x*y; ... y + diff(y,t) == beta*x*y]
eqs(t) =
To solve the system of differential equations, convert it to first-order differential equations by using the odeToVectorField
function.
V = odeToVectorField(eqs);
Next, convert the symbolic expression V
to a MATLAB function file by using matlabFunction
. The converted function in the file myODE.m
can be used without Symbolic Math Toolbox™.
matlabFunction(V,File="myODE")
ans = function_handle with value:
@myODE
Write Function in MATLAB
Write a MATLAB function named plotODESols
that solves the system of differential equations using ode45
. This function accepts the input parameters alpha
and beta
. Because these parameters might be interpreted as character vectors in your system, convert them to the double
data type. Finally, this function plots the solutions of the differential equations. Save it in the same directory as myODE.m
function.
type plotODESols
function plotODESols(alpha,beta) if ischar(alpha) alpha = double(string(alpha)); end if ischar(beta) beta = double(string(beta)); end tspan = linspace(0,20,200); [t,y] = ode45(@(t,Y) myODE(Y,alpha,beta),tspan,[50 100]); plot(t,y,"o-") xlabel("Time") ylabel("Population") end
You can use this function to create and deploy standalone application using MATLAB Compiler.
Create Standalone Application Using compiler.build.standaloneApplication
Build a standalone application using a programmatic approach. Alternatively, if you want to create a standalone application package using a graphical interface, see Create Standalone Application Using Standalone Application Compiler App (MATLAB Compiler).
Build the standalone application using the compiler.build.standaloneApplication
(MATLAB Compiler) function.
buildResults = compiler.build.standaloneApplication("plotODESols.m");
The buildResults
object contains information on the build type, generated files, included support packages, and build options. For details, see compiler.build.Results
(MATLAB Compiler).
The function generates the following files within a folder named plotODESolsstandaloneApplication
in your current working directory:
includedSupportPackages.txt
— Text file that lists all support files included in the application.plotODESols.exe
orplotODESols
— Executable file that has the.exe
extension if compiled on a Windows system, or no extension if compiled on Linux or macOS systems.run_plotODESols.sh
— Shell script file that sets the library path and executes the application. This file is only generated on Linux and macOS systems.mccExcludedFiles.log
— Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see Limitations (MATLAB Compiler).readme.txt
— Text file that contains information on deployment prerequisites and the list of files to package for deployment.requiredMCRProducts.txt
— Text file that contains product IDs of products required by MATLAB Runtime to run the application.unresolvedSymbols.txt
— Text file that contains information on unresolved symbols.
To test plotODESols
from MATLAB with the input parameters alpha = 0.01
and beta = 0.02
, navigate to the plotODESolsstandaloneApplication
folder from within the MATLAB desktop environment and execute one of the following commands based on your operating system:
Operating System | Test in MATLAB Command Window |
---|---|
Windows |
|
macOS |
|
Linux |
|
Create Standalone Application Installer Using compiler.package.installer
Create an installer using the buildResults
object as an input argument to the compiler.package.installer
function. By default, the installer is configured to download MATLAB Runtime from the web. You can modify this and specify additional options by using name-value arguments. For details, see compiler.package.installer
(MATLAB Compiler).
For example, include MATLAB Runtime in the installer. The function creates a new folder named plotODESolsinstaller
that contains the standalone application installer.
compiler.package.installer(buildResults,RuntimeDelivery="installer");
To install your application using an installer created by the compiler.package.installer
function, see Install Deployed Application (MATLAB Compiler). For example, in Windows® platform, you can query into the plotODESolsinstaller
folder location and double-click the file MyAppInstaller.exe
. Complete the installation by following the instructions in the installation wizard.
Run Standalone Application
In your system command prompt, navigate to the folder containing your standalone executable.
Run plotODESols
with the input arguments 0.01
and 0.02
by using one of the following commands based on your operating system:
Operating System | Command |
---|---|
Windows |
|
macOS | Using the shell script:
Using the executable:
|
Linux | Using the shell script:
Using the executable:
|
NOTE: To run the application without using the shell script on Linux and macOS, you must first add MATLAB Runtime to the library path. For details, see Set MATLAB Runtime Path for Deployment (MATLAB Compiler).
The application plots the solutions of the system of differential equations.
Deploy Standalone Application on Target Machines
Copy the plotODESolsstandaloneApplication
folder to a file location on all target machines where MATLAB Runtime is installed. Run the MATLAB generated standalone application on all target machines by using the executable in the plotODESolsstandaloneApplication
folder.
Alternatively, you can distribute the installer located in the plotODESolsinstaller
folder. This installer will install the application along with MATLAB Runtime, which allows you to run the application outside of MATLAB.
Tips
Instead of using the
compiler.build.standaloneApplication
(MATLAB Compiler) function to create a standalone application, you can also use the Standalone Application Compiler (MATLAB Compiler) app to create a standalone application.To produce a standalone application that does not launch a Windows command shell, use
compiler.build.standaloneWindowsApplication
(MATLAB Compiler).To specify additional compilation options, you can use the
mcc
(MATLAB Compiler) command to create a standalone application that does not include MATLAB Runtime or an installer.
See Also
Topics
- Download and Install MATLAB Runtime (MATLAB Compiler)
- Create Standalone Application from MATLAB (MATLAB Compiler)
- Create Standalone Application Using Standalone Application Compiler App (MATLAB Compiler)
- Set MATLAB Runtime Path for Deployment (MATLAB Compiler)
- Install Deployed Application (MATLAB Compiler)