Create Standalone Application from MATLAB Function
This example shows how to use MATLAB® Compiler™ to package a MATLAB® function into a standalone application. The target system only requires a MATLAB Runtime installation to run the application and does not require a licensed copy of MATLAB.
Supported Platforms: Windows®, Linux®, macOS
Create Function in MATLAB
Write a MATLAB function named magicsquare
that prints a magic square matrix at the command prompt. Save the function in a file named magicsquare.m
.
function m = magicsquare(n) if ischar(n) n=str2double(n); end m = magic(n); disp(m)
Test the function at the command prompt.
magicsquare(5)
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Create Standalone Application Using compiler.build.standaloneApplication
Use the compiler.build.standaloneApplication
function to create a standalone application from the MATLAB function.
appFile = "magicsquare.m";
buildResults = compiler.build.standaloneApplication(appFile);
You can specify additional options in the compiler.build
command by using name-value arguments. For details, see compiler.build.standaloneApplication
.
The buildResults
object contains information on the build type, generated files, included support packages, and build options. For details, see compiler.build.Results
.
The function generates the following files within a folder named magicsquarestandaloneApplication
in your current working directory:
includedSupportPackages.txt
— Text file that lists all support files included in the application.magicsquare.exe
ormagicsquare
— Executable file that has the.exe
extension if compiled on a Windows system, or no extension if compiled on Linux or macOS systems.run_magicsquare.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.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.
NOTE: The generated standalone executable does not include MATLAB Runtime or an installer. To create an installer using the buildResults
object, see Create Standalone Application Installer Using compiler.package.installer.
Test Standalone Application
To run magicsquare
from within MATLAB with the input argument 4
, navigate to the magicsquarestandaloneApplication
folder from within the MATLAB desktop environment and execute one of the following commands based on your operating system:
Windows
!magicsquare 4
Linux
!./magicsquare 4
macOS
system(['./run_magicsquare.sh ',matlabroot,' 4']);
To run your standalone application outside of the MATLAB desktop environment, see Run Standalone Application.
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.
compiler.package.installer(buildResults);
The function creates a new folder that contains the standalone application installer.
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
.
For example, to specify the installer name and include MATLAB Runtime in the installer, execute:
compiler.package.installer(buildResults, ... 'InstallerName','MyMagic_Install','RuntimeDelivery','installer');
Install Standalone Application
To install your application using an installer created by the compiler.package.installer
function, see Install Deployed Application.
Run Standalone Application
In your system command prompt, navigate to the folder containing your standalone executable.
Run magicsquare
with the input argument 5
by using one of the following commands based on your operating system:
Windows
magicsquare 5
Linux
Using the shell script:
./run_magicsquare.sh <MATLAB_RUNTIME_INSTALL_DIR> 5
NOTE: On Linux, the application uses software OpenGL™ by default. You can force hardware OpenGL by removing the path ${MCRROOT/sys/opengl/lib/glnxa64
from the shell script.
Using the executable:
./magicsquare 5
macOS
Using the shell script:
./run_magicsquare.sh <MATLAB_RUNTIME_INSTALL_DIR> 5
Using the executable:
./magicsquare.app/Contents/macOS/magicsquare 5
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.
The application outputs a 5-by-5 magic square in the console:
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
To create a command line shortcut for the application on Linux or macOS, use the alias command.
alias mymagic='/path/to/run_magicsquare.sh <MATLAB_RUNTIME_INSTALL_DIR>'
To run your application with the input argument 4
, type mymagic 4
in the terminal.
To make an alias permanent, append the command to the file ~/.bash_aliases
in a Bash shell or ~/.zprofile
in a Zsh shell. For example,
echo "alias mymagic='~/MATLAB/apps/run_magicsquare.sh /usr/local/MATLAB/MATLAB_Runtime/R2023a'" >> ~/.bash_aliases
Tips
Instead of using the
compiler.build.standaloneApplication
function to create a standalone application, you can also use the Application Compiler app to create a standalone application.To produce a standalone application that does not launch a Windows command shell, use
compiler.build.standaloneWindowsApplication
.You can also use the
mcc
command to produce a standalone application that does not include MATLAB Runtime or an installer.
See Also
compiler.build.standaloneApplication
| compiler.build.standaloneWindowsApplication
| compiler.package.installer
| Application Compiler | deploytool
| mcc