Specifying parameters for varargin
1 view (last 30 days)
Show older comments
I have a function that plots data using varargin as an input. Varargin can contain at least one of the following but nothing else - 'tomatoes', 'carrots', 'lettuce', 'blueberries', 'strawberries'. I have data relating to each one of these options that will be plotted. I can't figure out how to get it so the function will only accept inputs with some kind of combination of these. It has to be case insensitive so I know I have to use strcmpi, but I don't really know how to work with varargin
0 Comments
Answers (1)
Matthew Eicholtz
on 29 Feb 2016
Edited: Matthew Eicholtz
on 29 Feb 2016
Here's an example...
function fruitsandveggies(varargin)
validinputs = {'tomatoes', 'carrots', 'lettuce', 'blueberries', 'strawberries'};
mask = ismember(lower(varargin),lower(validinputs));
if ~all(mask)
error('Valid inputs are: %s',sprintf('%s ',validinputs{:}));
end
% insert additional code here
end
In this case, if the user enters one or more invalid inputs, the function gives an error.
fruitsandveggies('carrots') %works
fruitsandveggies('Strawberries','TOMATOES') %works
fruitsandveggies('carrots','lettuce','peas') %gives an error!
Is this what you were looking for?
0 Comments
See Also
Categories
Find more on Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!