Suppose that I have a function that has a required input (data
) and one optional input (op
). The op
can accept these strings: x
and y
. The default value of x and y (If the user doesn't insert a value for that) is 1
and 2
. But user can insert this value after selection of op
. As you can see, the value is related to the op
; therefore, if we add op
option, we should have its kind (x
or y
), but having the second value is optional for that (12
or any other value - I want to set a rule for that in the future. For instance, If the x
is selected, I can only select positive numbers for this value). For example:
my_func(data, 'op', 'x'); % the value of x in my code is 1.
my_func(data, 'op', 'x', 12); % the value of x in my code is 12.
my_func(data, 'op', 'y', 10); % the value of x in my code is 11.
my_func(data, 'op', 'x', 12, 'another_param', 0); % the value of x in my code is 12 (change the position of 'op')
To wrap up, this parameter can select two values. The first is its kind and the second (optional) is its value which is related to its kind. How can I implement this behavior in MATLAB using inputParser
?
More information about parameters:
op
can accept two kinds:x
ory
.If the user selects
x
, he can select related value (any value) for that (after thex
).If the user selects
y
, he can select related value (only positive values) for that (after they
).If the user doesn't mention second parameter for
x
, the default value is1
(selectingx
forop
option).If the user doesn't mention second parameter for
y
, the default value is2
(selectingy
forop
option).If the user doesn't mention
op
, the default is :x
and 1 as the value.After mentioning
op
and its kind, he has permission to select the value. There isn't anyway to select the value without mentioning theop
kind.