Specifying parameters for varargin
显示 更早的评论
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
回答(1 个)
Matthew Eicholtz
2016-2-29
编辑:Matthew Eicholtz
2016-2-29
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?
类别
在 帮助中心 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!