0

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 or y.

  • If the user selects x, he can select related value (any value) for that (after the x).

  • If the user selects y, he can select related value (only positive values) for that (after the y).

  • If the user doesn't mention second parameter for x, the default value is 1 (selecting x for op option).

  • If the user doesn't mention second parameter for y, the default value is 2 (selecting y for op 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 the op kind.

Community
  • 1
  • 1
Eghbal
  • 3,892
  • 13
  • 51
  • 112
  • 1
    It's not clear to me what sort of behavior you are trying to achieve. If 'op' can only accept two strings 'x' and 'y' the third input parameter should be skipped and input validation should be added for required input parameter 'op'. The fourth parameter as you show is an optional input parameter and the rest are name value pairs. – Some Guy Jun 22 '17 at 14:16
  • @SomeGuy. I added more information. – Eghbal Jun 22 '17 at 14:21
  • 1
    Is `op` even needed as an input argument here? Couldn't you just enter `'x', 12` or `'y', 10` by themselves? Generally, you should try to organize your inputs as parameter/value *pairs*. Could you add more detail about what these represent (i.e. what you are specifically trying to use this for)? – gnovice Jun 22 '17 at 14:51
  • @gnovice I added more information above. – Eghbal Jun 22 '17 at 14:59
  • 1
    Are you required to use that input format? Would you be okay with specifying the arguments after `op` as a cell array instead, so that you are dealing with pairs of inputs? I'm thinking of something like this: `my_func(data, 'op', {'x',12});` for setting a value or `my_func(data, 'op', {'x'});` for using a default value. – gnovice Jun 22 '17 at 15:09
  • @gnovice. I thought about this solution. Currently, I'm searching for a way to implement mentioned way. If I don't find a way to that, I'll use this way. – Eghbal Jun 22 '17 at 15:11

0 Answers0