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

조회 수: 1 (최근 30일)
Giuseppe
Giuseppe 2014년 5월 21일
댓글: Giuseppe 2014년 5월 21일
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