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.
入力引数
入力パーサー スキーム。inputParser オブジェクトとして指定します。
解析および検証を行う入力。コンマ区切りリストとして指定します。argList の要素には任意のデータ型を指定できます。入力パーサーは、引数を入力パーサー スキームに追加したときに指定した検証関数を使用して、引数の妥当性を判別します。
例: 'textA',13,mtxB
例: varargin{:}
拡張機能
スレッドベースの環境
MATLAB® の backgroundPool を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool を使用してコードを高速化します。
バージョン履歴
R2007a で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)