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

前端 未结 3 1116
傲寒
傲寒 2020-12-09 02:37

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 r

3条回答
  •  眼角桃花
    2020-12-09 03:06

    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;
    

提交回复
热议问题