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

1 vue (au cours des 30 derniers jours)
Giuseppe
Giuseppe le 21 Mai 2014
Commenté : Giuseppe le 21 Mai 2014
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.

Réponse acceptée

Cedric
Cedric le 21 Mai 2014
Modifié(e) : Cedric le 21 Mai 2014
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

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by