Create Standalone Application from MATLAB
Supported Platform: Windows®, Linux®, macOS
This example shows how to use MATLAB® Compiler™ to package the pre-written function that prints a magic square to the command prompt. The target system does not require a licensed copy of MATLAB to run the application.
You can create standalone applications using the following options:
Option | Purpose |
---|---|
Application Compiler | Use this app to produce an installer that installs both the standalone application and all required dependencies on a target system. |
compiler.build.standaloneApplication | Use this function to produce a standalone application that does
not include MATLAB Runtime or an installer. To produce a standalone application
that does not launch a Windows command shell, use |
compiler.package.installer | Use this function to produce an installer that installs both the standalone application and all required dependencies on a target system. |
mcc | Use this function to produce a standalone application that does not include MATLAB Runtime or an installer. |
Note
The application is not cross platform, and the executable type depends on the platform on which it was generated.
Create Function in MATLAB
In MATLAB, locate the MATLAB code that you want to deploy as a standalone application.
For this example, compile using the file magicsquare.m
located in
.matlabroot
\extern\examples\compiler
function m = magicsquare(n) if ischar(n) n=str2double(n); end m = magic(n) disp(m)
In the MATLAB command window, enter magicsquare(5);
.
The output is:
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 Application Compiler App
Package the function into a standalone application using the Application Compiler app. Alternatively, if you want to create a standalone application from the MATLAB command window using a programmatic approach, see Create Standalone Application Using compiler.build.standaloneApplication.
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Application Compiler.
Alternately, you can open the Application Compiler app by entering
applicationCompiler
at the MATLAB prompt.In the MATLAB Compiler project window, specify the main file of the MATLAB application that you want to deploy.
In the Main File section of the toolstrip, click
.
In the Add Files window, browse to
and selectmatlabroot
\extern\examples\compilermagicsquare.m
. Click Open.The function
magicsquare.m
is added to the list of main files.
Decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the two options in the Packaging Options section:
Runtime downloaded from web — Generates an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application.
Runtime included in package — Generates an installer that includes the MATLAB Runtime installer.
Customize the packaged application and its appearance:
Application information — Editable information about the deployed application. You can also customize the standalone applications appearance by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.
Command line input type options — Selection of input data types for the standalone application. For more information, see Determine Data Type of Command-Line Input (For Packaging Standalone Applications Only).
Additional installer options — Edit the default installation path for the generated installer and selecting custom logo. See Change the Installation Path .
Files required for your application to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.
Files installed for your end user — Files that are installed with your application. These files include:
Generated
readme.txt
Generated executable for the target platform
Additional runtime settings — Platform-specific options for controlling the generated executable. See Additional Runtime Settings.
Caution
On Windows operating systems, when creating a console only application, uncheck the box Do not display the Windows Command Shell (console) for execution. By default, this box is checked. If the box is checked, output from your console only application is not displayed. Since this example is a console only application, the box must be unchecked.
To generate the packaged application, click Package.
In the Save Project dialog box, specify the location to save the project.
In the Package dialog box, verify that Open output folder when process completes is selected.
When the packaging process is complete, examine the generated output.
Three folders are generated in the target folder location:
for_redistribution
,for_redistribution_files_only
, andfor_testing
.For further information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.
PackagingLog.html
— Log file generated by MATLAB Compiler.
To install your standalone application, see Install Standalone Application.
Create Standalone Application Using compiler.build.standaloneApplication
As an alternative to the Application Compiler app, you can create a standalone application using a programmatic approach. If you have already created an application using the Application Compiler, see Install Standalone Application.
In MATLAB, locate the MATLAB code that you want to deploy as a standalone application. For this example, compile using the file
magicsquare.m
located in
.matlabroot
\extern\examples\compilerappFile = fullfile(matlabroot,'extern','examples','compiler','magicsquare.m');
Build the standalone application using the
compiler.build.standaloneApplication
function.buildResults = compiler.build.standaloneApplication(appFile);
You can specify additional options in the
compiler.build
command by using name-value arguments. For details, seecompiler.build.standaloneApplication
.The
compiler.build.Results
objectbuildResults
contains information on the build type, generated files, included support packages, and build options.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 MATLAB Compiler 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.To run
magicsquare
from MATLAB with the input argument4
, navigate to themagicsquarestandaloneApplication
folder and execute one of the following commands based on your operating system:Operating System Test in MATLAB Command Window Windows !magicsquare 4
macOS system(['./run_magicsquare.sh ',matlabroot,' 4']);
Linux !./magicsquare 4
To run your standalone application outside of MATLAB, see Run Standalone Application.
Create Standalone Application Installer Using compiler.package.installer
As an alternative to the Application Compiler app, you can create a standalone application installer using a programmatic approach. If you have already created an application installer using the Application Compiler, see Install Standalone Application.
In MATLAB, locate the MATLAB code that you want to deploy as a standalone application. For this example, compile using the file
magicsquare.m
located in
.matlabroot
\extern\examples\compilerappFile = fullfile(matlabroot,'extern','examples','compiler','magicsquare.m');
Build the standalone application using the
compiler.build.standaloneApplication
function and save the output in acompiler.build.Results
object.buildResults = compiler.build.standaloneApplication(appFile);
Create an installer by using the
Results
objectbuildResults
as an input argument to thecompiler.package.installer
function.compiler.package.installer(buildResults);
The function creates a new folder that contains the standalone application installer.
You can specify additional options by using name-value arguments. For details, see
compiler.package.installer
.For example, specify the installer name and include MATLAB Runtime in the installer.
compiler.package.installer(buildResults, ... 'InstallerName','MyMagic_Install','RuntimeDelivery','installer');
Install Standalone Application
To install your application using an installer created by the Application
Compiler app or 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 argument5
by using one of the following commands based on your operating system:Operating System Command Windows magicsquare 5
Linux Using the shell script:
./run_magicsquare.sh
<MATLAB_RUNTIME_INSTALL_DIR>
5Using the executable:
./magicsquare 5
macOS Using the shell script:
./run_magicsquare.sh
<MATLAB_RUNTIME_INSTALL_DIR>
5Using 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 more information, 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
, typemymagic 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/R2022b'" >> ~/.bash_aliases
See Also
applicationCompiler
| deploytool
| compiler.build.standaloneApplication
| compiler.build.standaloneWindowsApplication
| compiler.package.installer
| mcc