Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get command line arguments in matlab

This is probably too easy, but I cannot google the answer for this: how can I get command line arguments in matlab script.

I run matlab as matlab -nodisplay -r "run('script.m')" and I want to return all arguments as a list. Something similar to python sys.argv. How can I do this?

I'm using Linux Mint and MATLAB 2015a.

like image 819
Sergey Ivanov Avatar asked Jun 12 '15 12:06

Sergey Ivanov


People also ask

How do you get input arguments in MATLAB?

Define a function that accepts a variable number of input arguments using varargin . The varargin argument is a cell array that contains the function inputs, where each input is in its own cell. Define a function that returns a variable number of output arguments using varargout .

How do you use Varargin?

Specify varargin by using lowercase characters. After any explicitly declared inputs, include varargin as the last input argument . When the function executes, varargin is a 1-by-N cell array, where N is the number of inputs that the function receives after the explicitly declared inputs.

How do you print a message in MATLAB?

There are three common ways: Type the name of a variable without a trailing semi-colon. Use the “disp” function. Use the “fprintf” function, which accepts a C printf-style formatting string.


1 Answers

I came up with a simple function that works on both Windows and Linux (Ubuntu):

function args = GetCommandLineArgs()

if isunix
    fid = fopen(['/proc/' num2str(feature('getpid')) '/cmdline'], 'r');
    args = textscan(fid, '%s', 'Delimiter', char(0));
    fclose(fid);
else
    kernel32WasAlreadyLoaded = libisloaded('kernel32');
    if ~kernel32WasAlreadyLoaded
        temporaryHeaderName = [gettempfolder '\GetCommandLineA.h'];
        dlmwrite(temporaryHeaderName, 'char* __stdcall GetCommandLineA(void);', '');
        loadlibrary('kernel32', temporaryHeaderName);
        delete(temporaryHeaderName);
    end
    args = textscan(calllib('kernel32', 'GetCommandLineA'), '%q');
    if ~kernel32WasAlreadyLoaded
        unloadlibrary kernel32;
    end
end

args = args{1};

On your sample call, it would return this:

>> GetCommandLineArgs

args = 

    '/[path-to-matlab-home-folder]/'
    '-nodisplay'
    '-r'
    'run('script.m')'

It returns a cell array of strings, where the first string is the path to MATLAB home folder (on Linux) or the full path to MATLAB executable (on Windows) and the others are the program arguments (if any).

How it works:

  • On Linux: the function gets the current Matlab process ID using the feature function (be aware it's an undocumented feature). And reads the /proc/[PID]/cmdline file, which on Linux gives the command line arguments of any process. The values are separated by the null character \0, hence the textscan with delimiter = char(0).

  • On Windows: the function calls GetCommandLineA, which returns the command line arguments on a string. Then it uses textscan to split the arguments on individual strings. The GetCommandLineA function is called using MATLAB's calllib. It requires a header file. Since we only want to use one function, it creates the header file on the fly on the temporary folder and deletes it after it's no longer needed. Also the function takes care not to unload the library in case it was already loaded (for example, if the calling script already loads it for some other purpose).

like image 177
Rafael Monteiro Avatar answered Sep 21 '22 08:09

Rafael Monteiro