Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

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.




Published with MATLAB® 7.11


  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。