Accessing data dictionary from Simulink model InitFcn callback

조회 수: 10 (최근 30일)
HaMo
HaMo 2018년 5월 15일
편집: HaMo 2018년 5월 22일
I have a Simulink model. I don't want to use variables from the base workspace, since the behavior could then be changed if someone else introduces variables with the same name. So I linked the model to a data dictionary. The problem is, the value of some variables are derived from other variables.
For instance:
in data dictionary: x=2, y = 5
derived parameters: z1 = x + y, z2 = x - y
I implemented the InitFcn callback with assignment for z1 and z2, however the initFcn does not have access to the variables from the data dictionary
Undefined function or variable 'x'. Variable 'x' does not exist.
Is there a way to access the Data Dictionary from a function or a script?

채택된 답변

TAB
TAB 2018년 5월 15일
편집: TAB 2018년 5월 15일
Simulink do not saves data dictionary parameters into base workspace.
To access Data Dictionary parameters you need to use APIs. Use code like below in InitFcn callback
dictObj = Simulink.data.dictionary.open('YourDD.sldd');
secObj = getSection(dictObj, 'YourSection Name');
enObjX = getEntry(secObj, 'x');
enObjY = getEntry(secObj, 'y');
temp_x = getValue(enObjX);
temp_y = getValue(enObjY);
% Your operation. Example: z = temp_x.Value + temp_y.Value;
close(dictObj);
clear dictObj secObj enObjX enObjY temp_x temp_y;
It is better to create a load script (m file) and call in InitFcn instead of writing the code in InitFcn.
  댓글 수: 1
HaMo
HaMo 2018년 5월 15일
편집: HaMo 2018년 5월 22일
Thanks, I just had to add the following lines of code for it to work:
If there is already an entry called z1 in data dictionary
entry = getEntry(secObj, 'z1');
entry.setValue(temp_x.Value + temp_y.Value);
Otherwise
addEntry(secObj, 'z1', temp_x.Value + temp_y.Value)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Manage Design Data에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by