How to add Extracted parameters from Excel file to a data dictionary as a Simulink Parameter Using matlab Code
37 次查看(过去 30 天)
显示 更早的评论
I want to add extracted data from excel file to data dictionary, thoses data include is described in a 11 rows with ParameterTitle, Value, Min, Max, Unity and description,
1) My first question is how can i add one row of my file to data dictionary as simulink parameter, i used this code but i got error !!
myDictionaryObj = Simulink.data.dictionary.create('myDictionary.sldd');
myDictionaryObj = Simulink.data.dictionary.open('myDictionary.sldd');
toolDataSectObj=getSection(myDictionaryObj,'Design Data');
ParameterFile = readtable('ParaFileSheet.xlsx');
param = Simulink.Parameter;
param.Value = cell2mat(ParameterFile.ReferenceValue(1));
param.Max = cell2mat(ParameterFile.Max(1));
param.Min = cell2mat(ParameterFile.Min(1));
param.DataType = 'auto';
param.DocUnits = cell2mat(ParameterFile.Unity(1));
param.Description = cell2mat(ParameterFile.Description(1));
addEntry(toolDataSectObj, cell2mat(ParameterFile.ParameterTitle(1)), param);
%% ERROR :
Invalid value specified for parameter. Value must be a numeric array, fi object, enumerated value, structure whose fields contain valid values, or an expression.
2) The second question is how can i add all parameters (11 rows) to my data dictionary using for loop ?
0 个评论
回答(1 个)
Rajanya
2024-9-24
I understand that you want to extract rows from an excel file as Simulink parameters and add them as entries to a Simulink data dictionary. I was able to reproduce the error by creating a demo excel sheet with the specified parameter columns (containing all the required entries for ‘Simulink.Parameter’) with 11 rows.
The error is because of a type mismatch caused when the extracted entry from the excel sheet does not meet the required types defined for that particular ‘Parameter’ object property. For example, the ‘Parameter.Value’ property can only accept numeric and boolean values, enums, structs, scalars, arrays and mathematical expressions.
I’ve created a sample excel file as attached to demonstrate the working.
The code to add all the 11 entries as parameters into a data dictionary is given below:
myDictionaryObj = Simulink.data.dictionary.create('myDictionary2.sldd');
myDictionaryObj = Simulink.data.dictionary.open('myDictionary2.sldd');
toolDataSectObj=getSection(myDictionaryObj,'Design Data');
ParameterFile = readtable('test.xlsx');
for i = 1:height(ParameterFile)
param = Simulink.Parameter;
param.Value = ParameterFile.Value(i);
param.Max = ParameterFile.Max(i);
param.Min = ParameterFile.Min(i);
param.Description = char(ParameterFile.Desc(i));
param.Unit = char(ParameterFile.Unity(i));
addEntry(toolDataSectObj, char(ParameterFile.Parameter(i)), param);
end
The dictionary, after creation, looks like this:
Please find more information on ‘Simulink.Parameter’ objects here: https://www.mathworks.com/help/releases/R2022a/simulink/slref/simulink.parameter.html
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Event Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!