Main Content

Standalone Applications and Arguments

You can pass arguments to standalone applications created using MATLAB® Compiler™ in the same way that you pass input arguments to any console-based application on the command line. Type the application name followed by one or more input arguments separated by spaces.

When you run a standalone application, the main program passes any command line arguments to your main function—the function you listed first when you created the application.

Pass Command Line Arguments to Applications

Refer to the following table for examples on passing different argument types, such as files, numbers or letters, matrices, and MATLAB variables to a standalone application named myapp.

To Pass....Use This Syntax....Notes
A file named helpfilemyapp path/to/helpfile

If the path to the file contains spaces, surround the path with double quotes.

Numbers or lettersmyapp 1 2 3 a b cDo not use commas or other separators between the numbers and letters you pass.
Matrices as inputmyapp "[1 2 3]" "[4 5 6]"Place double quotes around each matrix argument.
MATLAB variables

At the MATLAB Command Window, type:

for k=1:10
cmd = ['myapp ',string(k)];
system(cmd);
end
To pass a numeric MATLAB variable to a program as input, you must convert it to a character vector or string.

You can also call a standalone application from MATLAB. The following examples show how to pass the numbers and letters 1 2 3 a b c to a standalone application named myapp at the MATLAB command window.

Using system, dos, or unix

Specify the entire command as a character vector (including input arguments) using the system command.

system('myapp 1 2 3 a b c')

Using ! (Bang) Operator

Use the ! (bang) operator from within MATLAB.

!myapp 1 2 3 a b c

When you use the ! (bang) operator, the remainder of the input line is interpreted as a system command, so it is not possible to use MATLAB variables as arguments.

Using Windows Batch File

To run a standalone application with arguments by double-clicking it, you can create a batch file that calls the standalone application with the specified input arguments. For example, create runmyapp.bat with the following code.

 rem This is main.bat file which calls 
 rem myapp.exe with input parameters

 myapp "[1 2 3]" "[4 5 6]"
 @echo off
 pause

The last two lines of code in runmyapp.bat ensure that the window displaying your output stays open until you press a key.

Once you save this file, you run your code with the arguments specified above by double clicking the icon for runmyapp.bat.

Handle Input Arguments in MATLAB

By default, the input arguments you pass to your standalone application from a system prompt are received as character vector input. MATLAB code that accepts character vectors or strings as inputs does not need to be modified before packaging.

If your code expects the data in a different format (for example, double), you must do one or both of the following:

  • Convert the character vector input to the required format in your MATLAB code before processing it. For example, you can use the string and then double functions to convert the character vector input to numerical data.

  • Create your application with the behavior to automatically treat numeric inputs as MATLAB doubles by using the TreatInputsAsNumeric option with compiler.build.standaloneApplication, the Treat all inputs to the app as numeric MATLAB doubles option in the Application Compiler app, or the mcc -n flag. For details on the differences between packaging options, see Choose Deployment Option.

Here are two methods to convert input arguments to character vectors in your MATLAB code.

Method 1

Use ischar to test whether the input argument z is a character vector, and if so, convert it to a double.

function [x,y]=foo(z);

if ischar(z)
    z=double(string(z));
else
    z=z;
end
x=2*z
y=z^2;
disp(y)

Method 2

Use isdeployed to test whether the function is running in deployed mode, and if so, convert z to a double.

function [x,y]=foo(z);

if isdeployed
    z=double(string(z));
end
x=2*z
y=z^2;
disp(y)

Display Data to Screen Using MATLAB File

You cannot return values from your standalone application to the user. The only way to return values from compiled code is to either display it on the screen or store it in a file. By default, deployed applications output text to the standard output and standard error streams.

In order to have data displayed back to the screen, do one of the following:

  • Do not use semicolons to suppress commands that yield your return data.

  • Use the disp function to display the variable value, then redirect the output to other applications using command line redirects (the > operator) or pipes (||) on non-Windows® systems.

  • Display data visually using MATLAB graphics, such as with the plot function. Graphics are displayed in new windows when you run the application, and the application runs until all graphics are closed.

For example, create a program that plots a histogram of the number of times each letter in the alphabet appears in an input string. The function takes a single input and has zero outputs.

function freq(msg)
  % Remove spaces
  msg(msg == ' ') = [];

  % Convert to lower case, and map the letters to numbers.
  % a=1, b=2, etc.
  msg = lower(msg) - double('a') + 1;

  % Map non-letter characters to zero.
  msg(msg < 1) = 0;
  msg(msg > 26) = 0;

 % Display a histogram of the letter frequency in the message.
  count = hist(msg, 27);
  bar(0:26, count);
  labels = char([double('@'), double('a'):double('z')])';
  set(gca, 'XTick', 0:26+0.5, 'XTickLabel', labels);
 axis tight

Create a standalone application using compiler.build.standaloneApplication.

compiler.build.standaloneApplication('freq.m',Verbose=true);

Test the application at the MATLAB Command Window.

!freq "The quick brown fox jumps over the lazy dog!"

A histogram showing the frequency of letters in the string "The quick brown fox jumps over the lazy dog!"

See Also

| |

Related Topics