Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编译的matlab程序的getopts样式参数_Matlab - Fatal编程技术网

编译的matlab程序的getopts样式参数

编译的matlab程序的getopts样式参数,matlab,Matlab,除了使用包装器外,还有什么方法可以在使用mcc编译的matlab程序中使用getopts样式的参数,例如-v或-n3或-some_parameter=7?听起来像是在寻找对象 根据关于仅链接答案的评论进行编辑 下面是具有2个必需输入和2个可选参数的函数的一些示例用法: function hObj = dasImagePrototype(hAx, dasStruct, varargin) % ... defaultDepthUnit = getpref('HOTB', 'units_unitS

除了使用包装器外,还有什么方法可以在使用mcc编译的matlab程序中使用getopts样式的参数,例如-v或-n3或-some_parameter=7?

听起来像是在寻找对象

根据关于仅链接答案的评论进行编辑

下面是具有2个必需输入和2个可选参数的函数的一些示例用法:

function hObj = dasImagePrototype(hAx, dasStruct, varargin)

% ...

defaultDepthUnit = getpref('HOTB', 'units_unitSystem', 1);

p = inputParser;
p.addRequired('hAx', @(x) isa(x, 'matlab.graphics.axis.Axes'))
p.addRequired('dasStruct', @(x) isstruct(x) && isfield(x,'psd'))
p.addParameter('depthUnit', defaultDepthUnit, @(x) ismember(x,1:2))
p.addParameter('whichFbe', 1, @(x) floor(x)==x && x>0)
p.parse(hAx, dasStruct, varargin{:})

% access hAx & dasStruct directly since they're `Required` inputs
% but we still pass them to the input parser anyways to validate
hObj.hAx = hAx;
hObj.dasStruct = dasStruct; 

% note that since depthUnit & whichFbe are `Parameter` and not 
% `Required` inputs, we need to access them from the p object
hObj.depthUnit = p.Results.depthUnit;
hObj.whichFbe = p.Results.whichFbe;
如果您想为编译或未编译的MATLAB程序创建类似命令行的功能,那么使用对象(通常是输入处理的最佳方法)可能不是您的最佳选择。inputParser功能适合于遵循特定顺序的函数参数,即在参数列表中定位,并遵循参数/值对格式。典型的getopt样式功能涉及不需要遵循特定顺序的选项列表,并且两个选项都只有-v或带参数的选项,例如-n3或-some_参数=7格式

您可能需要在编译程序的最开始添加自己的输入解析例程。如前所述,当使用命令语法调用程序时,输入都作为一系列字符向量传递。如果您使用类似的方式定义函数:

然后按如下方式调用该函数:

getopt_example -v -n 3 --some_parameter=7
将导致varargin具有以下内容:

  1×4 cell array

    {'-v'}    {'-n'}    {'3'}    {'--some_parameter=7'}
然后,有必要在该单元格数组上迭代,解析选项,并根据需要将参数从字符向量转换为数值后,相应地设置/存储参数。有很多方法可以实现这一点,具体取决于您希望允许的选项格式。下面是一个解析上述示例参数类型的方法示例:

function getopt_example(varargin)

  % Define opts as {'name', has an argument, default value}:
  opts = {'v', false, false; ...              % No argument, default unset
          'n', true, 1; ...                   % double argument, default 1
          'some_parameter', true, uint8(0)};  % uint8 argument, default 0
  args = {};  % Captures any other arguments

  % Parse input argument list:
  inputIndex = 1;
  while (inputIndex <= nargin)

    opt = varargin{inputIndex};
    if strcmp(opt(1), '-') || strcmp(opt(1:2), '--')  % A command-line option

      % Get option and argument strings:
      opt = strip(opt, 'left', '-');
      [opt, arg] = strtok(opt, '=');
      [isOpt, optIndex] = ismember(opt, opts(:, 1));
      assert(isOpt, 'Invalid input option!');

      if opts{optIndex, 2}  % Has an argument

        if isempty(arg)  % Argument not included with '='
          assert(inputIndex < nargin, 'Missing input argument!');
          arg = varargin{inputIndex+1};
          inputIndex = inputIndex+2;
        else  % Argument included with '='
          arg = arg(2:end);
          assert(~isempty(arg), 'Missing input argument!');
          inputIndex = inputIndex+1;
        end

        % Convert argument to appropriate type:
        argType = class(opts{optIndex, 3});
        if ~strcmp(argType, 'char')
          arg = cast(str2double(arg), argType);
          assert(~isnan(arg), 'Invalid input option format!');
        end
        opts{optIndex, 3} = arg;

      else  % Has no argument

        opts{optIndex, 3} = true;
        inputIndex = inputIndex+1;

      end

    else  % A command-line argument

      args = [args {opt}];
      inputIndex = inputIndex+1;

    end

  end

  % Display results:
  disp(opts(:, [1 3]));
  disp(args);

end

当该程序由mcc编译时,可以使用getopt类型选项吗?从unix shell调用它,语法似乎是dasImagePrototype hAx dassstruct[depthUnit][whichbe],其中[…]表示可选参数,而我希望能够为参数分配标志,例如dasImagePrototype hAx dassstruct[-d depthUnit][whichbe]。当然,我可以专门制作一个shell包装器来实现这一点,但是有没有办法使用matlab编译器,比如C中的getopt allows?@user001:inputParser接受名称-值对。因此,您可以将d和f声明为可选参数的名称-遗憾的是,d不是合法名称。您的语法将如下所示:dasImagePrototype hAx dassstruct[d depthUnit][f whichbe]。谢谢。您的解决方案很健壮,填补了matlab本机参数解析功能的空白。此外,您对TMW中的命令与函数语法文档的引用有助于澄清调用行为。
function getopt_example(varargin)

  % Define opts as {'name', has an argument, default value}:
  opts = {'v', false, false; ...              % No argument, default unset
          'n', true, 1; ...                   % double argument, default 1
          'some_parameter', true, uint8(0)};  % uint8 argument, default 0
  args = {};  % Captures any other arguments

  % Parse input argument list:
  inputIndex = 1;
  while (inputIndex <= nargin)

    opt = varargin{inputIndex};
    if strcmp(opt(1), '-') || strcmp(opt(1:2), '--')  % A command-line option

      % Get option and argument strings:
      opt = strip(opt, 'left', '-');
      [opt, arg] = strtok(opt, '=');
      [isOpt, optIndex] = ismember(opt, opts(:, 1));
      assert(isOpt, 'Invalid input option!');

      if opts{optIndex, 2}  % Has an argument

        if isempty(arg)  % Argument not included with '='
          assert(inputIndex < nargin, 'Missing input argument!');
          arg = varargin{inputIndex+1};
          inputIndex = inputIndex+2;
        else  % Argument included with '='
          arg = arg(2:end);
          assert(~isempty(arg), 'Missing input argument!');
          inputIndex = inputIndex+1;
        end

        % Convert argument to appropriate type:
        argType = class(opts{optIndex, 3});
        if ~strcmp(argType, 'char')
          arg = cast(str2double(arg), argType);
          assert(~isnan(arg), 'Invalid input option format!');
        end
        opts{optIndex, 3} = arg;

      else  % Has no argument

        opts{optIndex, 3} = true;
        inputIndex = inputIndex+1;

      end

    else  % A command-line argument

      args = [args {opt}];
      inputIndex = inputIndex+1;

    end

  end

  % Display results:
  disp(opts(:, [1 3]));
  disp(args);

end
>> getopt_example -v -n 3 --some_parameter=7  % Sample inputs
    'v'                 [1]
    'n'                 [3]
    'some_parameter'    [7]

>> getopt_example -n 3 --some_parameter=7  % Omit -v option
    'v'                 [0]
    'n'                 [3]
    'some_parameter'    [7]

>> getopt_example -v -n --some_parameter=7  % Omit argument for -n option
Error using getopt_example (line 38)
Invalid input option format!

>> getopt_example -v -b --some_parameter=7  % Pass invalid -b option
Error using getopt_example (line 20)
Invalid input option!

>> getopt_example -v -n 3 --some_parameter=7 a1  % Pass additional argument a1
    'v'                 [1]
    'n'                 [3]
    'some_parameter'    [7]

    'a1'