Check if a number of varibles in a function satifies a condition.

1 次查看(过去 30 天)
Say for example I had the function
function [d,e] = examplefunction(a,b,c,d,e)
end
I would like to check if the inputs satify predetermined conditions such as, are all inputs numbers, do the inputs fall within their respective ranges, do more than one input equal zero.I was thinking I would use a variety of if else statements including the
tf = isa(obj,ClassName)
function. Then build a matrix of these logical values and see if any are false. My promblem is that I dont know if this is the most efficient way of doing this plus I dont know how I would compare multiple input varibles at once.
Any help would be appreciated.

采纳的回答

Cedric
Cedric 2014-5-21
编辑:Cedric 2014-5-21
There are already tools for performing these tasks. Check out the InputParser class [ ref, ref ]. It will allow you to declare three sets of arguments: required, optional, and param/value pairs. For each parameter, you will be able to define a check function, as well as a default value for all but required parameters.
EDIT: just as an example, here is the beginning of a function using an input parser
function [hFig, hAxis, hLegend, hPatches] = bar_varWidth( colWidth, ...
data, varargin )
% --- 1. Parse input parameters.
parser = inputParser ;
parser.StructExpand = true ;
parser.CaseSensitive = false ;
parser.addRequired( 'colWidth', @isnumeric ) ;
parser.addRequired( 'data', @isnumeric ) ;
parser.addParamValue( 'legend', [], @(x) ischar(x) || iscell(x) ) ;
parser.addParamValue( 'xTickLabel', [], @(x) isnumeric(x) || iscell(x) ) ;
parser.addParamValue( 'xTickLabelFormat', '%.2f', @ischar ) ;
parser.addParamValue( 'xTickLabelShiftThreshold', 10, @isnumeric ) ;
parser.addParamValue( 'xTickLabelAngle', 90, @isnumeric ) ;
parser.addParamValue( 'xTickLabelLineLength', [], @isnumeric ) ;
parser.parse( colWidth, data, varargin{:} ) ;
args = parser.Results ;
% --- 2. ...
and here all parameters are accessible as fields of args, e.g.
args.colWidth
args.legend

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by