Create Standalone Application from MATLAB - MATLAB & Simulink (original) (raw)
Main Content
Supported Platforms: Windows®, Linux®, macOS
This example shows how to use MATLAB® Compiler™ to package a 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.
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
.
copyfile(fullfile(matlabroot,'extern','examples','compiler','magicsquare.m');
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 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.
- Build the standalone application using the
compiler.build.standaloneApplication
function.
buildResults = compiler.build.standaloneApplication("magicsquare.m");
You can specify additional options in thecompiler.build
command by using name-value arguments. For details, see compiler.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 namedmagicsquarestandaloneApplication
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 files do not include an installer for the application orMATLAB Runtime. To create an installer using thebuildResults
object, see compiler.package.installer.
- To test
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
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
Tips
- To produce a standalone application that does not launch a Windows command shell, use compiler.build.standaloneWindowsApplication.
- To specify additional compilation options, you can use the mcc command to create a standalone application that does not include MATLAB Runtime or an installer.
See Also
compiler.build.standaloneApplication | compiler.build.standaloneWindowsApplication | compiler.build.Results | compiler.package.installer | Standalone Application Compiler | mcc