parse
関数入力の解析
説明
例
必要な入力が非負であることの検証
必須の入力が非負の数値スカラーであるかどうかをチェックする入力パーサー スキームを作成します。構文 @(x)
は 1 つの入力のある無名関数のハンドルを作成します。
p = inputParser;
argName = 'num';
validationFcn = @(x) (x > 0) && isnumeric(x) && isscalar(x);
addRequired(p,argName,validationFcn)
-1
などの無効な入力を解析します。
parse(p,-1)
The value of 'num' is invalid. It must satisfy the function: @(x)(x>0)&&isnumeric(x)&&isscalar(x).
入力の解析
必須およびオプションの関数入力を解析し検証します。
関数をファイル findArea.m
に作成します。関数 findArea
には入力引数 width
が必須で、可変数の追加入力を受け入れます。入力パーサー スキームは、次の引数の条件を指定します。
width
(必須の引数)。必須の引数は位置引数であるため、width
は関数findArea
の最初の引数でなければなりません。入力パーサーは、width
が正の数値スカラーであるかをチェックします。height
(オプション引数)。オプション引数は位置引数であるため、height
が関数findArea
の引数である場合、2 番目の引数でなければなりません。入力パーサーは、height
が正の数値スカラーであるかをチェックします。'units'
および関連付けられた値 (名前と値のペア)。名前と値のペアはオプションです。関数findArea
を呼び出すときに、位置引数の後に名前と値のペアを任意の順序で指定します。入力パーサーは、'units'
の値が string であるかをチェックします。'shape'
および関連付けられた値 (別の名前と値のペア)。入力パーサーは、'shape'
の値が配列expectedShapes
に含まれているかをチェックします。
function a = findArea(width,varargin) defaultHeight = 1; defaultUnits = 'inches'; defaultShape = 'rectangle'; expectedShapes = {'square','rectangle','parallelogram'}; p = inputParser; validScalarPosNum = @(x) isnumeric(x) && isscalar(x) && (x > 0); addRequired(p,'width',validScalarPosNum); addOptional(p,'height',defaultHeight,validScalarPosNum); addParameter(p,'units',defaultUnits,@isstring); addParameter(p,'shape',defaultShape,... @(x) any(validatestring(x,expectedShapes))); parse(p,width,varargin{:}); a = p.Results.width*p.Results.height; end
関数 findArea
を複数回呼び出します。入力パーサーは、これらの関数呼び出しのいずれでもエラーをスローしません。
a = findArea(7); a = findArea(7,3); a = findArea(13,'shape','square'); a = findArea(13,'units',"miles",'shape','square');
入力パーサー スキームに一致しない引数を指定して関数を呼び出します。width
入力に数値以外の値を指定します。
a = findArea('text')
Error using findArea (line 14) The value of 'width' is invalid. It must satisfy the function: @(x)isnumeric(x)&&isscalar(x)&&(x>0).
'shape'
にサポートされていない値を指定します。
a = findArea(4,12,'shape','circle')
Error using findArea (line 14) The value of 'shape' is invalid. Expected input to match one of these values: 'square', 'rectangle', 'parallelogram' The input, 'circle', did not match any of the valid values.
入力引数
p
— 入力パーサー スキーム
inputParser
オブジェクト
入力パーサー スキーム。inputParser
オブジェクトとして指定します。
argList
— 解析および検証を行う入力
コンマ区切りリスト
解析および検証を行う入力。コンマ区切りリストとして指定します。argList
の要素には任意のデータ型を指定できます。入力パーサーは、引数を入力パーサー スキームに追加したときに指定した検証関数を使用して、引数の妥当性を判別します。
例: 'textA',13,mtxB
例: varargin{:}
拡張機能
スレッドベースの環境
MATLAB® の backgroundPool
を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool
を使用してコードを高速化します。
バージョン履歴
R2007a で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)