addRequired
入力パーサー スキームに必須の位置引数を追加
説明
例
必須入力を追加
inputParser
オブジェクトを作成して、入力スキームに myinput
という名前の必須入力を追加します。
p = inputParser;
argName = 'myinput';
addRequired(p,argName)
入力値 7 を指定して関数 parse
を呼び出し、その結果を表示します。
parse(p,7) p.Results
ans = struct with fields:
myinput: 7
必要な入力が非負であることの検証
必須の入力が非負の数値スカラーであるかどうかをチェックする入力パーサー スキームを作成します。構文 @(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).
validateattributes
を使用した必須の入力の検証
inputParser
オブジェクトを作成し、validateattributes
を使用して検証関数を定義します。この検証関数は、必須の入力が正の偶数値であることをテストします。
p = inputParser; argName = 'evenPosNum'; validationFcn = @(x) validateattributes(x,{'numeric'},... {'even','positive'}); addRequired(p,argName,validationFcn)
入力文字ベクトルを解析します。入力が無効であるため、解析は失敗します。
parse(p,'hello')
The value of 'evenPosNum' is invalid. Expected input to be one of these types: double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64 Instead its type was char.
奇数を解析します。入力が無効であるため、解析は失敗します。
parse(p,13)
The value of 'evenPosNum' is invalid. Expected input to be even.
偶数の正の数値を解析します。解析はパスします。
parse(p,42)
入力引数
p
— 入力パーサー スキーム
inputParser
オブジェクト
入力パーサー スキーム。inputParser
オブジェクトとして指定します。
argName
— 入力引数の名前
文字ベクトル | string スカラー
入力引数の名前。文字ベクトルまたは string スカラーとして指定します。
例: 'firstName'
例: 'address'
データ型: char
| string
validationFcn
— 引数を検証する関数
関数ハンドル
引数を検証する関数。関数ハンドルとして指定します。
関数ハンドルは、true
または false
を返す、テストをパスする、あるいはエラーをスローする、のいずれかを行う関数に関連付けなければなりません。いずれのタイプの関数も、単一の入力引数を受け入れなければなりません。
例: @(s)isstring(s)
例: @(x)isnumeric(x)&&isscalar(x)
例: @(n)validateattributes(n,{'numeric'},{'nonnegative'})
データ型: function_handle
バージョン履歴
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)