Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

以编程方式将数据存储到字典中

数据字典可以存储 Simulink® 模型数据,还能提供比 MATLAB® 基础工作区或模型工作区更多的数据管理功能(请参阅什么是数据字典?)。要以编程方式与数据字典中的数据进行交互,请执行以下操作:

  1. 创建一个表示目标字典的 Simulink.data.Dictionary 对象。

  2. 创建一个表示目标分区(例如“设计数据”分区)的 Simulink.data.dictionary.Section 对象。使用此对象与该分区中存储的条目进行交互以及添加条目。

  3. (可选)创建几个 Simulink.data.dictionary.Entry 对象,分别表示目标分区中的每个条目。使用这些对象与目标分区中的各个条目进行交互。

要以编程方式访问变量以扫描模块参数值,请考虑使用 Simulink.SimulationInput 对象,而不是通过数据字典的编程接口修改变量。请参阅优化、估计和扫描模块参数值

要以编程方式与数据字典的 Embedded Coder 部分交互,请参阅Create Data Interface Configuration Programmatically (Embedded Coder)

向数据字典的“设计数据”分区添加条目

  1. 使用名为 dDataSectObjSimulink.data.dictionary.Section 对象表示数据字典 myDictionary_ex_API.sldd 的“设计数据”分区。

    myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd');
    dDataSectObj = getSection(myDictionaryObj,'Design Data');
    
  2. myDictionary_ex_API.sldd 的“设计数据”分区中,添加一个值为 237 的条目 myNewEntry

    addEntry(dDataSectObj,'myNewEntry',237)

重命名数据字典条目

重命名数据字典的“设计数据”、“配置”或“其他数据”部分中的条目。

  1. 使用名为 fuelFlowObjSimulink.data.dictionary.Entry 对象表示数据字典条目 fuelFlowfuelFlow 在数据字典 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. 使用名为 fuelFlowObjSimulink.data.dictionary.Entry 对象表示数据字典条目 fuelFlowfuelFlow 在数据字典 myDictionary_ex_API.sldd 中定义。

    myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd');
    dDataSectObj = getSection(myDictionaryObj,'Design Data');
    fuelFlowObj = getEntry(dDataSectObj,'fuelFlow');
  2. 将目标条目的值存储在一个临时变量中。将临时变量的值递增一。

    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

字典分区管理

数据字典以条目的形式将数据存储到分区中,默认情况下,所有字典都包含至少三个分区,分别为“设计数据”、“其他数据”和“配置”。使用 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);

请注意,此代码不会迁移用来定义模型变量的枚举数据类型的定义。如果您将枚举数据类型的模型变量导入数据字典但不迁移枚举类型定义,则字典具有较低的可移植性,并且在其他人使用时字典可能无法正常工作。要将枚举数据类型定义迁移到数据字典中,请参阅Enumerations in Data Dictionary

直接从外部文件向字典中导入

此示例说明如何使用自定义 MATLAB 函数直接从外部文件向数据字典中导入数据,而不用在基础工作区中创建或更改变量。

  1. 在 Microsoft® Excel® 工作簿的一个工作表中创建一个二维查找表。在工作表的左上方为两个断点和表提供名称。使用 B 列和第 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)

    其中有三个用来存储导入的断点和查找表的新条目。现在即可在 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.slddSimulink.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)

另请参阅

| | |

相关主题