mustBeA
説明
mustBeA(
は、value
,classNames
)value
を classNames
のクラス名リストと比較し、value
のクラスが名前付きクラスのいずれでもない場合、または名前付きクラスのいずれかのサブクラスでない場合に、エラーをスローします。この関数は値を返しません。
クラス サポート: すべての MATLAB® クラス
例
値が特定の型であるかどうかを判別
値がスカラー、あるいは string 型または uint8
型の配列であることを検証します。
a = 'red'; mustBeA(a,["string","uint8"])
Value must be of the following types: 'string' or 'uint8'.
mustBeA
によってテキスト値が string のみに制限されているため、文字ベクトルを渡すことは無効です。
b = [1 0 0]; mustBeA(b,["string","uint8"])
Value must be of the following types: 'string' or 'uint8'.
mustBeA
によって数値が uint8
のみに制限されているため、double の整数の配列を渡すことは無効です。
有効なサブクラス
サブクラスはスーパークラスとの "isa" 関係を満たします。そのため、指定されたいずれかのクラスのサブクラスである値は有効です。
uint8
のサブクラスを定義します。
classdef ColorSpec < uint8 methods function obj = ColorSpec(c) if nargin == 0 c = uint8(0); end obj = obj@uint8(c); end end end
ColorSpec
クラスのオブジェクトを作成します。
a = ColorSpec([1 0 0])
a = 1×3 ColorSpec: uint8 data: 1 0 0
ColorSpec
オブジェクトには uint8
データが含まれています。
mustBeA
を使用して ColorSpec
オブジェクトの有効性をテストします。次のステートメントではエラーが発生しません。
mustBeA(a,["string","uint8"])
入力引数
value
— 検証する値
スカラー | 配列
検証する値。スカラー、または MATLAB の任意の型またはユーザー定義型の配列として指定します。
classNames
— 1 つ以上のクラスの名前
データ型またはクラスの名前
1 つ以上のクラスの名前。string または文字ベクトルとして指定します。
例: ["double", "single"]
ヒント
mustBeA
は、プロパティと関数の引数の検証で使用されるように設計されています。
拡張機能
C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。
使用上の注意事項および制限事項:
コード生成では string 配列がサポートされていません。2 つ以上のクラスの名前を
mustBeA
に渡すには、文字ベクトルの cell 配列を使用します。
バージョン履歴
R2020b で導入
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)