以编程方式将数据存储到字典中
数据字典可以存储 Simulink® 模型数据,还能提供比 MATLAB® 基础工作区或模型工作区更多的数据管理功能(请参阅什么是数据字典?)。要以编程方式与数据字典中的数据进行交互,请执行以下操作:
创建一个表示目标字典的 Simulink.data.Dictionary 对象。
创建一个表示目标分区(例如 Design Data 分区)的 Simulink.data.dictionary.Section 对象。使用此对象与该分区中存储的条目进行交互以及添加条目。
(可选)创建几个 Simulink.data.dictionary.Entry 对象,分别表示目标分区中的每个条目。使用这些对象与目标分区中的各个条目进行交互。
要以编程方式访问变量以扫描模块参数值,请考虑使用 Simulink.SimulationInput 对象,而不是通过数据字典的编程接口修改变量。请参阅优化、估计和扫描模块参数值。
要以编程方式与数据字典的 Embedded Coder 部分交互,请参阅Create Code Definitions Programmatically(Embedded Coder)。向数据字典的 Design Data 分区添加条目
使用名为 dDataSectObj 的 Simulink.data.dictionary.Section 对象表示数据字典 myDictionary_ex_API.sldd 的 Design Data 分区。
myDictionaryObj = ...
Simulink.data.dictionary.open('myDictionary_ex_API.sldd');
dDataSectObj = getSection(myDictionaryObj,'Design Data');
在 myDictionary_ex_API.sldd 的 Design Data 分区中添加一个值为 237 的条目 myNewEntry。
addEntry(dDataSectObj,'myNewEntry',237)重命名数据字典条目
重命名数据字典的 Design Data、Configurations 或 Other Data 部分中的条目。
使用名为 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;