Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

プログラムによるディクショナリへのデータの格納

データ ディクショナリは Simulink® モデル データを格納して、MATLAB® ベース ワークスペースまたはモデル ワークスペースより多くのデータ管理機能を提供します (データ ディクショナリとはを参照)。プログラムでディクショナリ内のデータを対話的に操作するには、次を行います。

  1. ターゲットのディクショナリを表す Simulink.data.Dictionary オブジェクトを作成します。

  2. たとえば Design Data セクションなど、ターゲットのセクションを表す Simulink.data.dictionary.Section オブジェクトを作成します。オブジェクトを使用し、セクションに格納されているエントリを操作し、エントリを追加します。

  3. 必要に応じて、ターゲットのセクションでそれぞれがエントリを表す Simulink.data.dictionary.Entry オブジェクトを作成します。これらのオブジェクトを使用してターゲットのセクションで個々のエントリを操作します。

ブロック パラメーター値をスイープする目的でプログラムにより変数にアクセスするには、データ ディクショナリのプログラム インターフェイス経由で変数を変更する代わりに、Simulink.SimulationInput オブジェクトの使用を検討します。ブロック パラメーター値の最適化、推定およびスイープを参照してください。

プログラミングによりデータ ディクショナリの [Embedded Coder] セクションを操作する場合は、Create Data Interface Configuration Programmatically (Embedded Coder)を参照してください。

データ ディクショナリの [Design Data] セクションにエントリを追加

  1. データ ディクショナリ myDictionary_ex_API.sldd の [Design Data] セクションを dDataSectObj という名前の Simulink.data.dictionary.Section オブジェクトで表します。

    myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd');
    dDataSectObj = getSection(myDictionaryObj,'Design Data');
    
  2. myDictionary_ex_API.sldd の [Design Data] セクションに、値が 237myNewEntry というエントリを追加します。

    addEntry(dDataSectObj,'myNewEntry',237)

データ ディクショナリ エントリの名前を変更

データ ディクショナリの [データの設計]、[コンフィギュレーション] または [その他のデータ] セクションにあるエントリの名前を変更します。

  1. データ ディクショナリ エントリ fuelFlowfuelFlowObj という名前の Simulink.data.dictionary.Entry オブジェクトで表します。fuelFlow はデータ ディクショナリ myDictionary_ex_API.sldd で定義されています。

    myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd');
    dDataSectObj = getSection(myDictionaryObj,'Design Data');
    fuelFlowObj = getEntry(dDataSectObj,'fuelFlow');
  2. データ ディクショナリ エントリの名前を変更します。

    fuelFlowObj.Name = 'fuelFlowNew';

データ ディクショナリ エントリの値のインクリメント

  1. データ ディクショナリ エントリ fuelFlowfuelFlowObj という名前の Simulink.data.dictionary.Entry オブジェクトで表します。fuelFlow はデータ ディクショナリ myDictionary_ex_API.sldd で定義されています。

    myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd');
    dDataSectObj = getSection(myDictionaryObj,'Design Data');
    fuelFlowObj = getEntry(dDataSectObj,'fuelFlow');
  2. ターゲットのエントリの値を一時変数に格納します。一時変数の値を 1 だけインクリメントします。

    temp = getValue(fuelFlowObj);
    temp = temp+1;
  3. 一時変数を使用してターゲットのエントリの値を設定します。

    setValue(fuelFlowObj,temp)
    

データ ディクショナリ管理

Simulink.data.Dictionary オブジェクトを使用して、データ ディクショナリ全体を操作します。

目的使用
Simulink.data.Dictionary オブジェクトで既存のデータ ディクショナリを表す

Simulink.data.dictionary.open

Simulink.data.Dictionary オブジェクトでデータ ディクショナリを作成して表す

Simulink.data.dictionary.create

データ ディクショナリの操作

Simulink.data.Dictionary クラス

MATLAB ベース ワークスペースからデータ ディクショナリに変数をインポートする

importFromBaseWorkspace メソッド

データ ディクショナリに参照ディクショナリを追加する

addDataSource メソッド

データ ディクショナリから参照ディクショナリを削除する

removeDataSource メソッド

データ ディクショナリに対する変更を保存

saveChanges メソッド

データ ディクショナリに対する変更を破棄

discardChanges メソッド

データ ディクショナリに格納されているエントリのリストを表示する

listEntry メソッド

列挙型定義をデータ ディクショナリにインポート

importEnumTypes メソッド

データ ディクショナリのファイル名とパスを返す

filepath メソッド

モデル エクスプローラー ウィンドウにデータ ディクショナリを表示する

show メソッド

モデル エクスプローラー ウィンドウのデータ ディクショナリを非表示にする

hide メソッド

データ ディクショナリと Simulink.data.Dictionary オブジェクトの間の接続を閉じる

close メソッド

開いているデータ ディクショナリを特定する

Simulink.data.dictionary.getOpenDictionaryPaths

開いているすべてのデータ ディクショナリへのすべての接続を閉じる

Simulink.data.dictionary.closeAll

ディクショナリのセクションの管理

データ ディクショナリは、セクションに含まれているエントリとしてデータを格納します。また既定では、すべてのディクショナリには [Design Data]、[Other Data] および [Configurations] の少なくとも 3 つのセクションがあります。Simulink.data.dictionary.Section オブジェクトを使用して、データ ディクショナリのセクションを操作します。

目的使用
Section オブジェクトを使用してデータ ディクショナリのセクションを表す

getSection メソッド

データ ディクショナリのセクションを操作する

Simulink.data.dictionary.Section クラス

変数を MAT ファイルまたは MATLAB ファイルからデータ ディクショナリのセクションにインポートする

importFromFile メソッド

データ ディクショナリのセクションのエントリを MAT ファイルまたは MATLAB ファイルにエクスポートする

exportToFile メソッド

データ ディクショナリ セクションからエントリを削除

deleteEntry メソッド

データ ディクショナリのセクションの MATLAB 式を評価する

evalin メソッド

データ ディクショナリのセクションのエントリを検索する

find メソッド

データ ディクショナリのセクションにエントリが存在するかどうかを判別する

exist メソッド

ディクショナリのエントリの操作

データ ディクショナリに格納される変数は、ディクショナリのエントリと呼ばれます。エントリにはエントリが最後に変更された日時などのステータス情報を格納する追加のプロパティがあります。Simulink.data.dictionary.Entry オブジェクトを使用してデータ ディクショナリのエントリを操作します。

目的使用
Entry オブジェクトを使用してデータ ディクショナリのエントリを表す

getEntry メソッド

データ ディクショナリのエントリをセクションに追加して Entry オブジェクトを使用して表す

addEntry メソッド

データ ディクショナリのエントリを操作する

Simulink.data.dictionary.Entry クラス

新しい値をデータ ディクショナリのエントリに割り当てる

setValue メソッド

データ ディクショナリ エントリに対する変更を表示

showChanges メソッド

データ ディクショナリの変更を保存する

saveChanges メソッド

データ ディクショナリのエントリの変更を破棄する

discardChanges メソッド

データ ディクショナリのエントリを配列内で検索する

find メソッド

データ ディクショナリ エントリの値を返す

getValue メソッド

データ ディクショナリ エントリを削除

deleteEntry メソッド

ディクショナリ内に列挙型定義を格納する

Simulink.data.dictionary.EnumTypeDefinition クラス

データ ディクショナリの使用への移行

データ ディクショナリを使用すると、モデル データのプログラムによる操作が複雑化する可能性があります。モデルをディクショナリにリンクすると、以下が考えられます。

  • コマンド プロンプトで簡単なコマンドを使用してモデル データを操作することができなくなる可能性があります。代わりに、ディクショナリのプログラム インターフェイス (Simulink.data.Dictionary) を使用しなければなりません。

  • ディクショナリのプロパティ [ディクショナリからベース ワークスペースへのアクセスを有効にする] (ベース ワークスペースでの共有データの継続使用を参照) を選択すると、ターゲットのデータのストレージの場所に応じて、簡単なコマンドまたはプログラム インターフェイスのいずれかを使用しなければなりません。

ベース ワークスペースの使用からデータ ディクショナリの使用へと移行しやすくするには、これらの関数を使用することを検討してください。関数は、データのストレージの場所に関係なく、モデル データに対して操作を行います。

目的使用
Simulink モデルのコンテキスト内のデータ ディクショナリ エントリの値またはワークスペースの変数を変更する

Simulink.data.assigninGlobal

Simulink モデルのコンテキスト内の MATLAB 式を評価する

Simulink.data.evalinGlobal

Simulink モデルのコンテキスト内のデータ ディクショナリ エントリまたはワークスペースの変数の存在を判別する

Simulink.data.existsInGlobal

ディクショナリを使用する単一モデルへのプログラムによる移行

このコード例では、Simulink モデルのデータ ソースをベース ワークスペースから新しいデータ ディクショナリに変更する方法を示します。

% Define the data dictionary name
modelName = 'f14';
dictionaryName = 'myNewDictionary.sldd';
% Load the target model
load_system(modelName)
% Identify all model variables that are definied in the base workspace
varsToImport = Simulink.findVars(modelName,'SourceType','base workspace');
varNames = {varsToImport.Name};
% Create the data dictionary
dictionaryObj = Simulink.data.dictionary.create(dictionaryName);
% Import to the dictionary the model variables defined in the base workspace, and clear the variables from the base workspace
[importSuccess,importFailure] = importFromBaseWorkspace(dictionaryObj,...
 'varList',varNames,'clearWorkspaceVars',true);
% Link the dictionary to the model
set_param(modelName,'DataDictionary',dictionaryName);

このコードでは、モデル変数の定義に使用された列挙データ型の定義は移行されないことに注意してください。列挙データ型のモデル変数をデータ ディクショナリにインポートしても列挙型定義を移行しない場合、ディクショナリは移植性に欠け、他のユーザーが使用する場合に適切に機能しないことがあります。列挙データ型定義をデータ ディクショナリに移行するには、データ ディクショナリの列挙値を参照してください。

外部ファイルからディクショナリへの直接のインポート

この例では、カスタム MATLAB 関数を使用して、ベース ワークスペースで変数を作成または変更せずにデータを外部ファイルからデータ ディクショナリに直接インポートする方法を示します。

  1. Microsoft® Excel® ワークブックの 1 つのシート内に 2 次元ルックアップ テーブルを作成します。シートの左上隅を使用して、2 つのブレークポイントとテーブルの名前を示します。列 B と行 2 を使用して 2 つのブレークポイントを格納し、シートの残りの部分を使用してテーブルを格納します。たとえば、ルックアップ テーブルは次のようになります。

    An Excel spreadsheet. A2 contains the name of the first breakpoint. B1 contains the name of the second breakpoint. B2 contains the name of the table. Column B and row 2 contain the two breakpoints. The rest of the sheet stores the table.

    ワークブックを my2DLUT.xlsx として現在のフォルダーに保存します。

  2. このカスタム関数の定義を MATLAB ファイルにコピーして、このファイルを importLUTToDD.m として現在のフォルダーに保存します。

    function importLUTToDD(workbookFile,dictionaryName)
        % IMPORTLUTTODD(workbookFile,dictionaryName) imports data for a
        % two-dimensional lookup table from a workbook directly into a data
        % dictionary. The two-dimensional lookup table in the workbook can be
        % any size but must follow a standard format.
    
        % Read in the entire first sheet of the workbook.
        [data,names,~] = xlsread(workbookFile,1,'');
    
        % Divide the raw imported data into the breakpoints, the table, and their
        % names.
        % Assume breakpoint 1 is in the first column and breakpoint 2 is in the
        % first row.
        % Assume cells A2, B1, and B2 define the breakpoint names and table name.
        bkpt1 = data(2:end,1);
        bkpt2 = data(1,2:end);
        table = data(2:end,2:end);
        bkpt1Name = names{2,1};
        bkpt2Name = names{1,2};
        tableName = names{2,2};
    
        % Prepare to import to the Design Data section of the target data
        % dictionary.
        myDictionaryObj = Simulink.data.dictionary.open(dictionaryName);
        dDataSectObj = getSection(myDictionaryObj,'Design Data');
    
        % Create entries in the dictionary to store the imported breakpoints and
        % table. Name the entries using the breakpoint and table names imported
        % from the workbook.
        addEntry(dDataSectObj,bkpt1Name,bkpt1);
        addEntry(dDataSectObj,bkpt2Name,bkpt2);
        addEntry(dDataSectObj,tableName,table);
    
        % Save changes to the dictionary and close it.
        saveChanges(myDictionaryObj)
        close(myDictionaryObj)
    
  3. MATLAB コマンド プロンプトで、データ ディクショナリを作成してルックアップ テーブル データを格納します。

    myDictionaryObj = Simulink.data.dictionary.create('myLUTDD.sldd');
  4. カスタム関数を呼び出して、ルックアップ テーブルを新しいデータ ディクショナリにインポートします。

    importLUTToDD('my2DLUT.xlsx','myLUTDD.sldd')
  5. モデル エクスプローラーでデータ ディクショナリを開きます。

    show(myDictionaryObj)

    新しい 3 つのエントリに、インポートされたブレークポイントとルックアップ テーブルが格納されます。これらのエントリは、2-D Lookup Table ブロックで使用できます。

プログラムによるデータ ディクショナリの分割

データ ディクショナリを参照ディクショナリに分割するために、このコード例をテンプレートとして使用します。参照ディクショナリを使用して、大きなデータ ディクショナリを管理しやすくして、複数のモデルに役立つ標準化されたデータが含まれるようにします。

% Define the names of a parent data dictionary and two
% reference data dictionaries
parentDDName = 'myParentDictionary.sldd';
typesDDName = 'myTypesDictionary.sldd';
paramsDDName = 'myParamsDictionary.sldd';

% Create the parent data dictionary and a
% Simulink.data.Dictionary object to represent it
parentDD = Simulink.data.dictionary.create(parentDDName);

% Create a Simulink.data.dictionary.Section object to represent 
% the Design Data section of the parent dictionary
designData_parentDD = getSection(parentDD,'Design Data');

% Import some data to the parent dictionary from the file partDD_Data_ex_API.m
importFromFile(designData_parentDD,'partDD_Data_ex_API.m');

% Create two reference dictionaries
Simulink.data.dictionary.create(typesDDName);
Simulink.data.dictionary.create(paramsDDName);

% Create a reference dictionary hierarchy by adding reference dictionaries 
% to the parent dictionary
addDataSource(parentDD,typesDDName);
addDataSource(parentDD,paramsDDName);

% Migrate all Simulink.Parameter objects from the parent data dictionary to
% a reference dictionary
paramEntries = find(designData_parentDD,'-value','-class','Simulink.Parameter');
for i = 1:length(paramEntries)
    paramEntries(i).DataSource = 'myParamsDictionary.sldd';
end

% Migrate all Simulink.NumericType objects from the parent data dictionary
% to a reference dictionary
typeEntries = find(designData_parentDD,'-value','-class','Simulink.NumericType');
for i = 1:length(typeEntries)
    typeEntries(i).DataSource = 'myTypesDictionary.sldd';
end

% Save all changes to the parent data dictionary
saveChanges(parentDD)

ディクショナリ内に保存されているコンフィギュレーション セットの変更

コンフィギュレーション セット (Simulink.ConfigSet オブジェクト) をディクショナリの [コンフィギュレーション] セクションに格納できます。セット内のコンフィギュレーション パラメーターの設定をプログラムによって変更するには、次の手順を実行します。

  1. (ディクショナリのエントリである) コンフィギュレーション セットを表す Simulink.data.dictionary.Entry オブジェクトを作成します。たとえば、ディクショナリの名前が myData.sldd で、Simulink.ConfigSet オブジェクトの名前が myConfigs であると仮定します。

    dictionaryObj = Simulink.data.dictionary.open('myData.sldd');
    configsSectObj = getSection(dictionaryObj,'Configurations');
    entryObj = getEntry(configsSectObj,'myConfigs');

  2. ターゲット Simulink.ConfigSet オブジェクトのコピーを一時変数に保存します。

    temp = getValue(entryObj);

  3. 一時変数で、ターゲットのコンフィギュレーション パラメーターを変更します (この場合、[終了時間]20 に設定します)。

    set_param(temp,'StopTime','20');

  4. 一時変数を使用して、ディクショナリ内のコンフィギュレーション セットを上書きします。

    setValue(entryObj,temp);

  5. ディクショナリに対して行った変更を保存します。

    saveChanges(dictionaryObj)

参考

| | |

関連するトピック