How do I parse two input arguments for matching dimensions with Matlab Input Parser?

조회 수: 14 (최근 30일)
I use the Matlab Inputparser Class to validate function input, this is a minimal example:
function C = multiplyMatrix(A, B)
p = inputParser;
addRequired(p, 'A', @isnumeric); % Line A
addRequired(p, 'B', @isnumeric);
parse(p, A, B);
if size(A, 2) ~= size(B, 1) % Line B
error('Size mismatch.');
end
C = A*B;
end
How do I integrate tests spanning more than one variable (i.e. the if-statement in Line B) in the concept of the Matlab Inputparser Class? I only found out how to create tests regarding one variable (see Line A).
I am also happy about comments about the usage of this Parser in total.
(I had asked this question on stackoverflow.com before, but I feel like this is the better place to ask.)

채택된 답변

Dima Lisin
Dima Lisin 2014년 7월 26일
Unfortunately, you cannot do that inside inputParser. For each parameter you can only supply a function that validates just that parameter. Any tests involving more than one parameter have to be done separately, after you call parse().
  댓글 수: 4
Lukas
Lukas 2014년 7월 29일
The inputParser is indeed very useful. Since the errors are clear and the notation is widely accepted, I would like to remain within the inputParser framework.
This is a working improvisation:
function C = multiplyMatrix(A, B)
p = inputParser;
A_B = {A, B};
addRequired(p, 'A', @isnumeric);
addRequired(p, 'B', @isnumeric);
addRequired(p, 'A_B', @(x) (size(x{1}, 2) == size(x{2}, 1)));
parse(p, A, B, A_B);
C = A*B;
end
This is a possible way how it could look like with the additional function addCombination(). The additional function would have to be applied after evaluation of all other add*() functions. Thus, default values can be compared within combinations as well:
function C = multiplyMatrix(A, B)
p = inputParser;
addRequired(p, 'A', @isnumeric);
addRequired(p, 'B', @isnumeric);
addCombination(p, {'A', 'B'}, @(x, y) (size(x, 2) == size(y, 1)));
parse(p, A, B);
C = A*B;
end
Thank you for your detailed answer.
Daniele Busi
Daniele Busi 2018년 9월 16일
The suggested addCombination method can only be included inside a subclass. This subclass should do the job:
classdef inputParserEx < inputParser
properties
combofuncs
combovars
end
methods
function p = inputParserEx
p.combofuncs = cell(0);
p.combovars = cell(0);
end
function addCombination(p,vars,validateFunc)
assert(isa(p,'inputParserEx'))
assert(iscellstr(vars) && isvector(vars))
assert(isa(validateFunc,'function_handle'))
assert(nargin(validateFunc) == numel(vars));
p.combofuncs{end+1} = validateFunc;
p.combovars(end+1,1) = {numel(vars)};
p.combovars(end,2:numel(vars)+1) = {vars{:}};
end
function parse(p,varargin)
assert(isa(p,'inputParserEx'))
parse@inputParser(p,varargin{:});
for c = 1:numel(p.combofuncs)
args = cell(1,p.combovars{c,1});
for v = 1:p.combovars{c,1}
assert(isfield(p.Results,p.combovars{c,1+v}),['invalid combination number ' num2str(c)]);
args{v} = p.Results.(p.combovars{c,1+v});
end
assert(p.combofuncs{c}(args{:}),['condition number ' num2str(c) ' not fulfilled']);
end
end
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by