Create Standalone Application from MATLAB Function - MATLAB & Simulink (original) (raw)

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.

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:

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

Linux

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

Linux

Using the shell script:

./run_magicsquare.sh 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:

macOS

Using the shell script:

./run_magicsquare.sh 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 '

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

See Also

compiler.build.standaloneApplication | compiler.build.standaloneWindowsApplication | compiler.package.installer | Application Compiler | deploytool | mcc