【问题标题】:Get command line arguments in matlab在matlab中获取命令行参数
【发布时间】:2015-08-28 10:50:55
【问题描述】:

这可能太简单了,但我无法通过谷歌搜索答案:如何在 matlab 脚本中获取命令行参数。

我以 matlab -nodisplay -r "run('script.m')" 的身份运行 matlab,我想将所有参数作为列表返回。类似于 python sys.argv 的东西。我怎样才能做到这一点?

我正在使用 Linux Mint 和 MATLAB 2015a。

【问题讨论】:

  • 也许你可以举一个例子,让那些不熟悉你在 python 中引用的功能的人得到什么?
  • 通常,参数可以被接受或传递,而不是返回。你在这里争论之后是什么意思?您是指函数的值还是要在内部传递参数?
  • 如果你想要一个如何从命令行调用matlab的例子,你可以查看this question
  • 不,我只想从matlab 访问所有命令行参数。在这个例子中,我想作为一个矩阵检索 [-nodisplay, -r, "run('script.m')"]
  • 事实上,我想在命令末尾传递一些额外的参数,并且我想从脚本中访问这些参数。

标签: matlab command-line


【解决方案1】:

我不知道方向解决方案(如内置函数)。 但是,您可以使用以下解决方法之一:

1.方法

这仅适用于 Linux:

创建一个文件pid_wrapper.m,内容如下:

function [] = pid_wrapper( parent_pid )

[~, matlab_pid] = system(['pgrep -P' num2str(parent_pid)]);
matlab_pid = strtrim(matlab_pid);
[~, matlab_args] = system(['ps -h -ocommand ' num2str(matlab_pid)]);
matlab_args = strsplit(strtrim(matlab_args));
disp(matlab_args);

% call your script with the extracted arguments in matlab_args
% ...

end

像这样调用 MATLAB:

matlab -nodisplay -r "pid_wrapper($$)"

这会将 MATLAB 父进程(即启动 MATLAB 的 shell)的进程 ID 传递给 wrapper。然后可以使用它来找出子 MATLAB 进程及其命令行参数,然后您可以在 matlab_args 中访问它们。

2。方法

此方法与操作系统无关,并不会真正找出命令行参数,但由于您的目标是将其他参数传递给脚本,因此它可能对您有用。

创建一个文件vararg_wrapper.m,内容如下:

function [] = wrapper( varargin )

% all parameters can be accessed in varargin
for i=1:nargin
    disp(varargin{i});
end

% call your script with the supplied parameters 
% ...

end

像这样调用 MATLAB:

matlab -nodisplay -r "vararg_wrapper('first_param', 'second_param')"

这会将{'first_param', 'second_param'} 传递给vararg_wrapper,然后您可以将其转发到您的脚本。

【讨论】:

    【解决方案2】:

    我想出了一个适用于 Windows 和 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};
    

    在您的示例调用中,它将返回:

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

    它返回一个字符串元胞数组,其中第一个字符串是 MATLAB 主文件夹的路径(在 Linux 上)或 MATLAB 可执行文件的完整路径(在 Windows 上),其他字符串是程序参数(如果有)。

    工作原理:

    • 在 Linux 上: 该函数使用 feature 函数获取当前的 Matlab 进程 ID(请注意这是一个未记录的特性)。并读取/proc/[PID]/cmdline 文件,on Linux 提供任何进程的命令行参数。这些值由空字符 \0 分隔,因此 textscan 带有分隔符 = char(0)。

    • 在 Windows 上:该函数调用 GetCommandLineA,它返回字符串中的命令行参数。然后它使用textscan 将参数拆分为单个字符串。使用 MATLAB 的 calllib 调用 GetCommandLineA 函数。它需要一个头文件。由于我们只想使用一个函数,它会在临时文件夹中动态创建头文件,并在不再需要后将其删除。此外,该函数会注意不要卸载已加载的库(例如,如果调用脚本已出于其他目的加载它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 2014-01-18
      • 1970-01-01
      • 2014-01-03
      • 2018-06-14
      • 2019-06-20
      • 1970-01-01
      相关资源
      最近更新 更多