How can I pass command line arguments to a standalone MATLAB executable running on Linux/Unix?
Asked Answered
C

3

28

How can I pass command line arguments to a standalone MATLAB executable running on Linux/UNIX?

I need to compile my MATLAB script as a standalone file that can be run on a machine without MATLAB present. It needs to be able to work in a way similar to C's argv[], where you do the following:

Command line:

myfile argument1.txt argument2.txt

where argument 1 and 2 are the input and output files.

The exact syntax doesn't matter, but it should work like argv[]. What is a function that could do this?


What I mean is that I have MATLAB on my computer, but I need to make an standalone executable that can run on Unix systems WITHOUT MATLAB on those computers (it's a cluster, that only has MATLAB on one node). I need to find a way to make the varargin function without having MATLAB installed on the computer that's running the program. If I can tell MATLAB to put the MATLAB library in each executable, that's OK, as long as it's a complete standalone package.

Conversational answered 26/7, 2010 at 14:0 Comment(0)
P
27

The MATLAB website has a worked-through example with instructions on how to compile a simple application and how to deploy it on another computer. In essence, you need to install the MATLAB Compiler Runtime together with your application; the installer for the runtime should already be present in your MATLAB compiler installation.

To pass command-line arguments to a MATLAB executable, you define a single MATLAB function in the executable: the arguments to the function are taken from the command line parameters (the first command-line parameter is the first argument, and so on).

For example, create a file echo.m with the following contents:

function exitcode = echo(a, b)

display(a);
display(b);

exitcode = 0;

You can then compile this file and run it with echo 1 2 3, and it will print a=1 b=2.

Note that, when arguments are taken from the command line, they are passed to the function as strings, so you need to convert them to numbers using the str2num function. For instance:

function rc = display_product(a, b)

a = str2num(a);
b = str2num(b);

display(a*b);

rc = 0;
Permittivity answered 1/6, 2011 at 14:1 Comment(4)
What about the returned value?Prosper
I tried to follow the link and ended up at a generic mathworks page. In an attempt to find a new, updated url so I could edit the answer with it, I was asked for a mathworks login just to access the documentation. I think Mathworks is subconsciously begging for people to abandon them for octave, R, and numpy.Puto
@Puto I have replaced the broken link with a new one; but I do not know, (1) whether this is the same content that I linked to in 2001, and (2) whether a MathWorks account is needed to view the page (I concur with your final remark...)Permittivity
I have changed this link to one that does not contain a version number. This link should not require a license to view (you only need a license to read documentation for previous versions of MATLAB, hence a link with a version number, if visible now without a license, will no longer be visible when a new version is released).Strontia
S
4

I had the same problem and searched for a generic solution to the problem that in a script, the arguments are passed as strings, but needed as scalars or vectors. Suppose you have the following function

function myfunc(arg1, arg2, varargs)
end

and you maybe want to be able to call it like

myfunc(1, [1 2 3], 'optional1', 2)

and also like

myfunc('1', '[1 2 3]', 'optional1', '2')

so that you can compile it and use it on the command line like

myfunc 1 '[1 2 3]' optional1 2

For this, I wrote the following function:

function r=evalArguments(parser, arguments)
    % Evaluates parsed arguments' values.  
    % Given a parser containing parsed arguments, all string values of the
    % arguments specified by parameter "arguments" are evaluated 
    % and the complete results is returned in a new struct.

    r = parser.Results;
    for j=1:length(arguments)
        argValue = r.(arguments{j});
        if ischar(argValue)
            r.(arguments{j}) = eval(argValue);
        end
    end
end

Then I am able to use the inputParser in myfunc like this:

p = inputParser;
p.addRequired('arg1');
p.addRequired('arg2');
p.addParameter('optional1', 0);
p.parse(arg1, arg2, varargin{:});
nonStringArguments = {'arg1', 'arg2', 'optional1'};
args = evalArguments(p, nonStringArguments);
...
x = args.arg1;
y = args.arg2;
z = args.optional1;

As I did not find an out-of-the-box way to do it, I post this solution here and hope it may also be useful to others. If there is a better way to achieve this, please let me know.

Smoothen answered 3/5, 2016 at 8:42 Comment(1)
If isdeployed you can expect the input arguments to be strings, otherwise you don’t need to do the conversion.Strontia
A
0

You can't really run MATLAB on a computer without MATLAB. You can install the MCR (MATLAB Component Runtime), which is free to install, and run MATLAB programs. To build a 'standalone' executable you'll also need the MATLAB compiler.

Once you have all that. MATLAB has all the facilities you need to handle command line arguments in the way your require.

Ankeny answered 26/7, 2010 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.