以编程方式将数据存储到字典中 |
您所在的位置:网站首页 › matlab怎么引用另一个文件的数据 › 以编程方式将数据存储到字典中 |
以编程方式将数据存储到字典中 数据字典可以存储 Simulink® 模型数据,还能提供比 MATLAB® 基础工作区或模型工作区更多的数据管理功能(请参阅什么是数据字典?)。要以编程方式与数据字典中的数据进行交互,请执行以下操作:
创建一个表示目标字典的 Simulink.data.Dictionary 对象。 创建一个表示目标分区(例如“设计数据”分区)的 Simulink.data.dictionary.Section 对象。使用此对象与该分区中存储的条目进行交互以及添加条目。 (可选)创建几个 Simulink.data.dictionary.Entry 对象,分别表示目标分区中的每个条目。使用这些对象与目标分区中的各个条目进行交互。
要以编程方式访问变量以扫描模块参数值,请考虑使用 Simulink.SimulationInput 对象,而不是通过数据字典的编程接口修改变量。请参阅优化、估计和扫描模块参数值。 要以编程方式与数据字典的 Embedded Coder 部分交互,请参阅Create Data Interface Configuration Programmatically (Embedded Coder)。 向数据字典的“设计数据”分区添加条目
使用名为 dDataSectObj 的 Simulink.data.dictionary.Section 对象表示数据字典 myDictionary_ex_API.sldd 的“设计数据”分区。 myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd'); dDataSectObj = getSection(myDictionaryObj,'Design Data');在 myDictionary_ex_API.sldd 的“设计数据”分区中,添加一个值为 237 的条目 myNewEntry。 addEntry(dDataSectObj,'myNewEntry',237)重命名数据字典条目 重命名数据字典的“设计数据”、“配置”或“其他数据”部分中的条目。
使用名为 fuelFlowObj 的 Simulink.data.dictionary.Entry 对象表示数据字典条目 fuelFlow。fuelFlow 在数据字典 myDictionary_ex_API.sldd 中定义。 myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd'); dDataSectObj = getSection(myDictionaryObj,'Design Data'); fuelFlowObj = getEntry(dDataSectObj,'fuelFlow');重命名该数据字典条目。 fuelFlowObj.Name = 'fuelFlowNew';递增数据字典条目的值
使用名为 fuelFlowObj 的 Simulink.data.dictionary.Entry 对象表示数据字典条目 fuelFlow。fuelFlow 在数据字典 myDictionary_ex_API.sldd 中定义。 myDictionaryObj = Simulink.data.dictionary.open('myDictionary_ex_API.sldd'); dDataSectObj = getSection(myDictionaryObj,'Design Data'); fuelFlowObj = getEntry(dDataSectObj,'fuelFlow');将目标条目的值存储在一个临时变量中。将临时变量的值递增一。 temp = getValue(fuelFlowObj); temp = temp+1;使用临时变量设置目标条目的值。 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 模型的数据源从 MATLAB 基础工作区更改为新的数据字典,请使用以下示例代码作为模板。 % Define the model name and the data dictionary name modelName = 'f14'; dictionaryName = 'myNewDictionary.sldd'; % Load the target model load_system(modelName); % Identify all model variables that are defined 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 函数直接从外部文件向数据字典中导入数据,而不用在基础工作区中创建或更改变量。 在 Microsoft® Excel® 工作簿的一个工作表中创建一个二维查找表。在工作表的左上方为两个断点和表提供名称。使用 B 列和第 2 行存储两个断点,并使用工作表的其余部分存储表。例如,您的查找表可能如下所示:
使用文件名 my2DLUT.xlsx 将该工作簿保存到当前文件夹中。 将以下自定义函数定义复制到一个 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)在 MATLAB 命令提示符下创建一个数据字典,以存储查找表数据。 myDictionaryObj = Simulink.data.dictionary.create('myLUTDD.sldd');调用自定义函数,将您的查找表导入到新的数据字典中。 importLUTToDD('my2DLUT.xlsx','myLUTDD.sldd')在模型资源管理器中打开该数据字典。 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 对象)。要以编程方式更改该配置集中配置参数的设置,请执行下列操作:
创建一个表示该配置集的 Simulink.data.dictionary.Entry 对象(它是字典中的一个条目)。例如,假设字典的名称是 myData.sldd,Simulink.ConfigSet 对象的名称是 myConfigs。 dictionaryObj = Simulink.data.dictionary.open('myData.sldd'); configsSectObj = getSection(dictionaryObj,'Configurations'); entryObj = getEntry(configsSectObj,'myConfigs'); 在临时变量中存储目标 Simulink.ConfigSet 对象的副本。 temp = getValue(entryObj); 在临时变量中,修改目标配置参数(在本例中,将停止时间设置为 20)。 set_param(temp,'StopTime','20'); 使用临时变量覆盖字典中的配置集。 setValue(entryObj,temp); 保存对字典所做的更改。 saveChanges(dictionaryObj) 另请参阅 Simulink.findVars | set_param | Simulink.data.dictionary.setupWorkerCache | Simulink.data.dictionary.cleanupWorkerCache 相关主题Enumerations in Data Dictionary迁移模型引用层次结构以使用字典什么是数据字典?优化、估计和扫描模块参数值 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |