How to partially validate arguments?

조회 수: 6 (최근 30일)
Andreas Urbán
Andreas Urbán 2022년 3월 21일
편집: Andreas Urbán 2022년 3월 21일
Rewriting some legacy code to arguments block style I run into cases where I should validate some arguments and pass the rest as is to other functions. This is about what I would like to accomplish:
function f1(positional, optional_positional, options, varargin)
arguments
positional;
optional_positional = 0;
% options validated by f1
options.f1_option = true;
end
arguments (Repeating)
% arguments without validation, to be validated by f2
varargin;
end
% Doing things with validated arguments goes here.
% Eventually passing the non-validated arguments to another function.
f2(varargin{:});
end
Above leads to an error:
Function argument definition error in f1. Functions with positional and name-value arguments must define positional arguments first.
So it would seem what I'm trying to do isn't exactly supported. Did I make a mistake somewhere? Is there another way of doing it?
Edit: What I'm after is very similar to inputParser KeppUnmatched option: Input parser for functions - MATLAB - MathWorks Nordic

채택된 답변

Bruno Luong
Bruno Luong 2022년 3월 21일
편집: Bruno Luong 2022년 3월 21일
I believe the issue is that your argument isn't self consistent.
When you use syntax such as
optional_positional = 1
That means you can call the function with single argument
f1(0);
MATLAB won't be able to know what is the varargin, it is started from 4th input argument, or 2th argument, 3rd argument?
You simply cannot at tell MATLAB some function arguments has default value when they are not passed, and at the same time still have varargin. It is not problem of not supporting, it's rather a logical of which argument corresponds to which that is ambigeous.
  댓글 수: 1
Andreas Urbán
Andreas Urbán 2022년 3월 21일
I appreciate the second opinion. Makes much sense.
Given varargin is pairs of arguments, and that there is only one positional, and that arguments block allows for name=value argument passing syntax, it is technically possible for Matlab to find an unambiguous solution for some calls. I can relate to not bother supporting it.
Trying a few more things I think I have an alternate design that is feasible for the rewriting I'm doing. Will post it soon.

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

추가 답변 (1개)

Andreas Urbán
Andreas Urbán 2022년 3월 21일
편집: Andreas Urbán 2022년 3월 21일
As Bruno points out there are possible ambiguities how to interpret any function call when mixing optional positional with varargin and the likes.
With that in mind and working on my code some more, I found myself using this solution:
function f1(positional, options)
arguments
positional;
% optional_positional NOT positional anymore
% Refactoring suggestion: f1(0, 1) -> f1(0, 'optional_positional', 1)
options.optional_positional = 0;
options.f1_option = true;
% similar to varargin
% Refactoring suggestion: f1(0, args{:}) -> f1(0, 'f2Args', args)
options.f2Args cell = {};
end
% Passing non-validated arguments to another function.
f2(options.f2Args{:});
end

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by