Loren on the Art of MATLAB

January 6th, 2011

MATLAB Data Types as Arguments to Standalone Applications

Guest blogger Peter Webb returns with another in an occasional series of postings about application deployment.

Contents

When you create a standalone executable from a MATLAB function with MATLAB Compiler, the generated main program has a limited ability to process command line arguments as strings. In a previous posting, I presented techniques to handle string arguments and to turn them into numeric scalars. This week, I'll show you how to handle more complex arguments in your standalone executables.

MATLAB-Type Arguments

MATLAB's data types encompass far more than strings and scalars. This example demonstrates how to pass cell arrays and function handles to a standalone executable.

The function cellops takes two inputs: a cell array of data and a function to apply to each cell in the cell array. cellops calls cellfun twice: once to apply the function to the cell array and a second time to display the results.

This function works as a standalone executable because it calls eval on its inputs if they are strings. Therefore, the strings on the command line must be valid MATLAB expressions.

function cellops(celldata, fcn)
  % If either if the inputs is a string, assume it is a MATLAB
  % expression, and call EVAL on it.
  if ischar(celldata)
      celldata = eval(celldata);
  end
  if ischar(fcn)
      fcn = eval(fcn);
  end
  % Apply the function to the cell array.
  output = cellfun(fcn, celldata, 'UniformOutput', false);
  % Display all the results.
  cellfun(@disp, output);

Compile the MATLAB function:

mcc -mv cellops

Then, pass cellops a cell array and a function handle, as in the following two examples.

Example 1

Use cellops to compute the mean of two random datasets. The first argument is a MATLAB expression that creates a cell array containing two vectors of random numbers, and the second argument creates a MATLAB function handle for the function mean.

cellops "{randn(10,1), randn(20,1)}" "@mean"

Example 2

Search for positive values in three datasets. The first argument is a cell array containing the three datasets -- three vectors of random numbers -- and the second is an anonymous function that extracts the positive numbers from its input.

cellops "{randn(10,1), randn(20,1), randn(30,1)}" "@(x) x(x>0)"

Notice the arguments to cellops must be MATLAB expressions with non-empty results. If you enter a statement, like x = {23, 17, 81}, eval will process it, but since MATLAB statements have no result, you'll receive an error.

 cellops "x={23, 17, 81}" "@mean"
 ??? Error: The expression to the left of the equals sign is not a valid
     target for an assignment.
 Error in ==> cellops at 5

The first call to eval, celldata = eval(celldata);, generated this error. It isn't possible to assign the non-existant result of eval to the variable celldata.

The second argument to cellops is a function handle. When creating function handles with eval, you must ensure that your function handles refer to functions included in the deployed application. Generally speaking, this means you can use builtin functions, functions in the MATLAB toolbox, anonymous functions and functions you've explicitly added with -a. I'll discuss this point in a later post on the depfun function.

Your Arguments Don't Have to be Strings

I hope these examples demonstrate how to increase the sophistication of your standalone executables' argument processing. Even though standalone executables only receive strings from the command line, you can turn those strings into many different kinds of data with a few extra lines of MATLAB code.

I leave you with exercise for the reader: how would you pass a MATLAB structure to a standalone executable? I have a few ideas, but none of them are really elegant. Post your suggestions here.


Get the MATLAB code

Published with MATLAB® 7.11

5 Responses to “MATLAB Data Types as Arguments to Standalone Applications”

  1. Aurelien Queffurust replied on :

    Hi !

    I am not very surprised that there is not a lof of answers. I must admit that I never build standalones which expect cells or structures as input.
    And I think that typing in a DOS windows:

    
    cellops "{randn(10,1), randn(20,1)}" "@mean"
    

    is not very easy for an end-user. In this case , I will write these lines in a bat/batch file ;)

    It already happened that I wrote codes which expect structures as input but these structures are previously saved in a MAT-file. So my current workaround is to get a MAT-file name as input and to load it as a structure if required.

    I am very impatient to see which tricks can be used !!

  2. Anshul replied on :

    This is slightly off-topic but in MATLAB 2010b, when I compile matlab code to a standalone with

    mcc -R -nodisplay …

    I get an annoying output of ‘NO DISPLAY’ to stdout before the program exits. This wasnt happening in 2009b. The code is the same. The mcc command I am using is the same. The reason it is annoying is that the program outputs some stuff to stdout which I pipe to gzip and I dont want the NO DISPLAY to show up in the gzipped file.

    Any idea what might be causing this and how to get rid of it.

    Thanks!

  3. Anshul replied on :

    Nevermind … I guess there is an error in the documentation.

    The ‘NO DISPLAY’ message seems to go away if you use

    mcc -R nodisplay …

    instead of

    mcc -R -nodisplay … (this is what the documentation says)

  4. Sree replied on :

    Hi,

    Can I distribute my standalone and MATLAB Runtime to an end user who does not have a valid MATLAB License? Does the distribution of MATLAB Runtime involve any license issues or legal problems?

  5. Peter Webb replied on :

    Sree,

    Generally speaking, as long as you’ve got a valid license for one of the deployment tools (MATLAB Compiler or one of the Builders) you can redistribute the MCR (the MATLAB Compiler Runtime) to anyone who needs it to run one of your applications. You can’t put it up on the Web though, for just anyone to grab. The people you distribute it to do not have to have a MATLAB license.

    The specific terms and permissions are governed by your license agreement (see the deployment addendum), which you should consult if you have further questions.


MathWorks
Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.