matlab 数据字典,以编程方式将数据存储到字典中
以編程方式將數據存儲到字典中
數據字典可以存儲 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;
使用臨時變量設置目標條目的值。
setValue(fuelFlowObj,temp)數據字典管理
使用 Simulink.data.Dictionary 對象與整個數據字典進行交互。
目的使用使用 Simulink.data.Dictionary 對象表示現有數據字典
使用 Simulink.data.Dictionary 對象創建和表示數據字典
從 MATLAB 基礎工作區向數據字典中導入變量
向數據字典添加引用字典
保存對數據字典所做的更改
放棄對數據字典所做的更改
查看數據字典中存儲的條目列表
向數據字典中導入枚舉類型定義
返回數據字典的文件名和路徑
在模型資源管理器窗口中顯示數據字典
在模型資源管理器窗口中隱藏數據字典
關閉數據字典與 Simulink.data.Dictionary 對象的連接字典分區管理
數據字典以條目的形式將數據存儲到分區中,默認情況下,所有字典都包含至少三個分區,分別為 Design Data、Other Data 和 Configurations。使用 Simulink.data.dictionary.Section 對象與數據字典分區進行交互。
目的使用使用 Section 對象表示數據字典分區。
從 MAT 文件或 MATLAB 文件中將變量導入到數據字典分區中
將數據字典節中的條目導出到 MAT 文件或 MATLAB 文件中
從數據字典分區中刪除條目
計算數據字典分區中的 MATLAB 表達式
在數據字典分區中搜索條目
確定條目是否存在于數據字典分區中字典條目操作
數據字典中存儲的每個變量被稱為字典中的一個條目。條目還有其他一些屬性用于存儲狀態信息,例如上次修改條目的日期和時間。使用 Simulink.data.dictionary.Entry 對象處理數據字典條目。
目的使用使用 Entry 對象表示數據字典條目
將數據字典條目添加到分區中并使用 Entry 對象表示它
為數據字典條目指定新值
顯示對數據字典條目所做的更改
保存對數據字典條目所做的更改
放棄對數據字典條目所做的更改
在數據字典條目數組中進行搜索
返回數據字典條目的值轉移為使用數據字典
使用數據字典可能會導致通過編程方式與模型數據進行交互的操作變得復雜。如果您將模型鏈接到字典:
您不能再在命令提示符下使用簡單命令與模型數據進行交互。在這種情況下,您必須使用字典的編程接口 (Simulink.data.Dictionary)。
當您選擇字典屬性 Enable dictionary access to base workspace(請參閱繼續使用基礎工作區的共享數據)時,根據目標數據的存儲位置,您必須使用簡單命令或編程接口。
為了幫助從使用基礎工作區轉換到使用數據字典,請考慮使用下列函數。無論模型數據存儲在哪里,這些函數都可作用于這些模型數據。
目的使用在 Simulink 模型上下文中更改數據字典條目或工作區變量的值
在 Simulink 模型上下文中計算 MATLAB 表達式
在 Simulink 模型上下文中確定是否存在數據字典條目或工作區變量以編程方式遷移單個模型以使用字典
要將 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)對存儲在字典中的配置集進行更改
您可以在字典的 Configurations 部分中存儲配置集(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);
在臨時變量中,修改目標配置參數(在本例中,將 Stop time 設置為 20)。
set_param(temp,'StopTime','20');
使用臨時變量覆蓋字典中的配置集。
setValue(entryObj,temp);
保存對字典所做的更改。
saveChanges(dictionaryObj)
另請參閱
相關主題
總結
以上是生活随笔為你收集整理的matlab 数据字典,以编程方式将数据存储到字典中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 1860 Currency Ex
- 下一篇: 抗击海冰 地理信息系统来帮忙