Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
inputParser

Subject: inputParser

From: Andy

Date: 25 Jan, 2011 19:44:04

Message: 1 of 1

I'm trying to write a function that needs very flexible calling syntax. I've been looking at the inputParser for the first time, and it's been very helpful, but I don't see the ability to do the following.

Suppose you have a function myFun which has one optional argument. The optional argument can be either 'opt1' or 'opt2':

function myFun(varargin)
  p = inputParser;
  validOpts = {'opt1' 'opt2'};
  p.addOptional('optional','opt1',@(x)any(strcmpi(x,validOpts)));
end

But what if, when the 'opt2' is chosen, you also want to specify an additional input parameter? So I would want the following to be valid calls:

> myFun % no opts
> myFun('opt1') % option 1
> myFun('opt2',4) % option 2 with additional parameter
> myFun('opt1','opt2',4) % should ERROR; 'opt1' and 'opt2' should be exclusive

Other than doing this by hand (i.e., checking p.Results.opt1 and p.Results.opt2 for simultaneous nonempty entry and adding an additional optional input for the parameter 4), is there a way to do this with the built in inputParser class?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us