このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。
データ クラスの定義
この例では、Simulink® データ クラスをサブクラス化する方法を示します。
MATLAB® クラス構文を使用して、パッケージにデータ クラスを作成します。必要に応じて、データ クラスにプロパティを割り当て、ストレージ クラスを定義します。
例を使用したデータ クラスの定義
フォルダー
にあるmatlabroot
/toolbox/simulink/simdemos/dataclasses+SimulinkDemos
データ クラス パッケージを表示します (開く)。このパッケージには定義済みのデータ クラスが含まれています。
データ クラスを定義する場所にフォルダーをコピーします。
フォルダー
+mypkg
の名前を変更して、その親フォルダーを MATLAB パスに追加します。データ クラス定義を変更します。
手動によるデータ クラスの定義
パッケージ フォルダー
+mypkg
を作成して、その親フォルダーを MATLAB パスに追加します。+mypkg
の内部にクラス フォルダー@Parameter
および@Signal
を作成します。メモ
Simulink では、データ クラスは
+Package/@Class
フォルダー内に定義する必要があります。@Parameter
フォルダー内に MATLAB ファイルParameter.m
を作成して編集のために開きます。MATLAB クラス構文を使用して
Simulink.Parameter
のサブクラスであるデータ クラスを定義します。classdef Parameter < Simulink.Parameter end % classdef
Parameter
または Signal
以外のカスタム クラス名を使用するには、カスタム名を使用してクラス フォルダーに名前をつけます。たとえば、クラス mypkg.myParameter
を定義するには次を行います。
Simulink.Parameter
またはSimulink.Signal
のサブクラスとしてデータ クラスを定義します。classdef myParameter < Simulink.Parameter end % classdef
クラス定義で、コンストラクター メソッドに
myParameter
またはmySignal
という名前をつけます。クラス定義を含むクラス フォルダーに
@myParameter
または@mySignal
という名前をつけます。
オプション: プロパティをデータ クラスに追加する
properties
と end
のキーワードは、プロパティ定義ブロックを囲んでいます。
classdef Parameter < Simulink.Parameter properties % Unconstrained property type Prop1 = []; end properties(PropertyType = 'logical scalar') Prop2 = false; end properties(PropertyType = 'char') Prop3 = ''; end properties(PropertyType = 'char',... AllowedValues = {'red'; 'green'; 'blue'}) Prop4 = 'red'; end end % classdef
Simulink.Parameter
、Simulink.Signal
または Simulink.CustomStorageClassAttributes
のサブクラスにプロパティを追加する場合、以下のプロパティ タイプを指定できます。
プロパティ タイプ | 構文 |
---|---|
double 型 | properties(PropertyType = 'double scalar') |
int32 型 | properties(PropertyType = 'int32 scalar') |
論理型 | properties(PropertyType = 'logical scalar') |
文字ベクトル (char) | properties(PropertyType = 'char') |
取りうる値を一部に制限された文字ベクトル | properties(PropertyType = 'char', AllowedValues = {'a', 'b', 'c'}) |
PropertyType
ではなく MATLAB プロパティの検証 (プロパティ値の検証を参照) を使用する場合、プロパティはクラスのダイアログ ボックスの編集フィールドとして表示されます。PropertyType
および AllowedValues
を使用する場合は、プロパティ ダイアログ ボックスには次が表示されます。
logical スカラー プロパティ向けのチェック ボックス。
文字ベクトルおよび
AllowedValues
向けのドロップダウン メニュー。
オプション: データ クラスに初期化コードを追加する
データ クラス内にコンストラクターを追加すると、クラスがインスタンス化されるときに初期化処理を実行できます。追加されたコンストラクターは、入力引数を必須とすることはできません。
この例では、コンストラクターはオプションの入力引数に基づいてオブジェクトの値 obj
を初期化します。
classdef Parameter < Simulink.Parameter methods function obj = Parameter(optionalValue) if (nargin == 1) obj.Value = optionalValue; end end end % methods end % classdef
オプション: ストレージ クラスの定義
setupCoderInfo
メソッドを使用して、クラスの CoderInfo
オブジェクトを設定します。次に、useLocalCustomStorageClasses
メソッドへの呼び出しを作成して、カスタム ストレージ クラス デザイナーを開きます。
データ クラス内のコンストラクターで
useLocalCustomStorageClasses
メソッドを呼び出します。classdef Parameter < Simulink.Parameter methods function setupCoderInfo(obj) useLocalCustomStorageClasses(obj, 'mypkg'); obj.CoderInfo.StorageClass = 'Custom'; end end % methods end % classdef
パッケージに対するカスタム ストレージ クラス デザイナーを開きます。
cscdesigner('mypkg')
ストレージ クラスを定義します。
オプション: ストレージ クラスのカスタム属性の定義
MATLAB ファイル
myCustomAttribs.m
を作成して編集用に開きます。このファイルを+mypkg/@myCustomAttribs
フォルダーに保存します。ここで、+mypkg
は@Parameter
フォルダーと@Signal
フォルダーを含むフォルダーです。MATLAB クラス構文を使用して
Simulink.CustomStorageClassAttributes
のサブクラスを定義します。たとえば、元の識別子を使用してデータを定義しながら、生成コード内でデータの代わりの名前を与えるストレージ クラスを考えてみます。classdef myCustomAttribs < Simulink.CustomStorageClassAttributes properties(PropertyType = 'char') AlternateName = ''; end end % classdef
isAddressable
メソッドの既定の実装をオーバーライドして、ストレージ クラスを書き込み可能にするかどうかを決定します。classdef myCustomAttribs < Simulink.CustomStorageClassAttributes properties(PropertyType = 'logical scalar') IsAlternateNameInstanceSpecific = true; end methods function retVal = isAddressable(hObj, hCSCDefn, hData) retVal = false; end end % methods end % classdef
getInstanceSpecificProps
メソッドの既定の実装をオーバーライドします。例については、以下のオーバーライド関数を参照してください。
function props = getInstanceSpecificProps(hObj) % GETINSTANCESPECIFICPROPERTIES Return instance-specific properties % (custom attributes that can be modified on each data object). if hObj.IsStructNameInstanceSpecific props = findprop(hObj, 'StructName'); else props = []; end end
function props = getInstanceSpecificProps(hObj) % GETINSTANCESPECIFICPROPERTIES Return instance-specific properties % (custom attributes that can be modified on each data object). props = []; if hObj.IsOwnerInstanceSpecific ptmp = findprop(hObj, 'Owner'); props = [props; ptmp]; end if hObj.IsDefinitionFileInstanceSpecific ptmp = findprop(hObj, 'DefinitionFile'); props = [props; ptmp]; end if hObj.IsPersistenceLevelInstanceSpecific ptmp = findprop(hObj, 'PersistenceLevel'); props = [props; ptmp]; end end
メモ
これはオプションの手順です。既定の設定では、すべてのカスタム属性がインスタンス固有で、データ オブジェクトごとに変更できます。ただし、どのプロパティをインスタンス固有になるように許可するかは制限できます。
getIdentifiersForInstance
メソッドの既定の実装をオーバーライドして、データ クラスのオブジェクトの識別子を定義します。メモ
既定の実装では、このメソッドはデータ オブジェクトの名前または識別子をクエリし、生成コードでその識別子を使用します。このメソッドをオーバーライドすることで、生成コード内のデータ オブジェクトの識別子を制御できます。
classdef myCustomAttribs < Simulink.CustomStorageClassAttributes properties(PropertyType = 'char') GetFunction = ''; SetFunction = ''; end methods function retVal = getIdentifiersForInstance(hCSCAttrib,... hCSCDefn, hData, identifier) retVal = struct('GetFunction',... hData.CoderInfo.CustomAttributes.GetFunction, ... 'SetFunction', hData.CoderInfo.CustomAttributes.SetFunction); end% end % methods end % classdef
グループ化されたストレージ クラスを使用する場合は、
getIdentifiersForGroup
メソッドの既定の実装をオーバーライドして、生成コード内のグループの識別子を指定します。例については、フォルダー
のmatlabroot
\toolbox\simulink\simulink\dataclasses\+Simulink\@CSCTypeAttributes_FlatStructureCSCTypeAttributes_FlatStructure.m
を参照してください (開く)。