Whay not working saveChanges(DD) in mscript?

조회 수: 3 (최근 30일)
sua
sua 2023년 8월 14일
답변: Githin George 2024년 12월 4일
I opened DD in mscript, set the value of a particular field in the parameter item to a new value, and then updated DD with saveChanges() api
When you check the parameter values in the Matlab console window, it seems to be updated well
When I open the actual DD, the value doesn't change
Why is that?
I'll leave my mscript at the bottom. Need help
---
function updateParametervalue()
parameters = evalin('base','parameters');
DD = evalin('base','ParamDD');
if isempty(DD)
disp('Parameter of DataDictionary is empty.');
else
DataSecObj = getSection(DD, 'Design Data');
DdVarList = find(DataSecObj);
DdFile = evalin('base','DdFile');
idx_DataSource = strcmp({DdVarList(:).DataSource}, DdFile);
DdVarNameAll = {DdVarList(:).Name};
DdVarName = DdVarNameAll(idx_DataSource);
num_iter = length(DdVarName);
paramName = parameters{1};
param2 = parameters{2};
param3 = parameters{3};
param4 = parameters{4};
for idx_var = 1:num_iter
VarPropEntry = getEntry(DataSecObj, DdVarName{idx_var});
value = getValue(VarPropEntry);
if ~isa(value, 'Simulink.Parameter')
continue;
end
if VarPropEntry.DataSource ~= DdFile
continue;
end
if strcmp(paramName, VarPropEntry.Name)
value.param2Property = param2;
if(strcmp(param4, 'null'))
value.param4Property = '';
else
value.param4Property = param4
end
if ~isempty(param3)
dataType = value.DataType;
end
saveChanges(DD);
break;
end
end
end
end
  댓글 수: 1
Yash
Yash 2024년 8월 29일
Hi Sua,
Can you please provide the information regarding the Variable "ParamDD" ? Is it a Simulink.data.Dictionary object?
If yes, please share the SLDD file and the caller code here, so that we can reproduce and resolve the issue.

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

답변 (1개)

Githin George
Githin George 2024년 12월 4일
Hi Sua,
I could find some issues in the script, and I have fixed it. I am making a few assumptions along the way (check the code comments).
Summary of the Changes Made:
  • Within the for loop, I am extracting the value of the SLDD parameter with the name matching with “parameters{1}” and modifying with the values provided in the input value “parameters”.
  • Use the “setValue” function to write the modified “Simulink.Parameter” value into the SLDD. Refer to the documentation below.
  • Saving the changes to the SLDD file using saveChanges.
function updateParametervalue()
% available in base workspace
parameters = evalin('base','parameters'); % Contains new data to modify the properties of data in the SLDD with name parameters{1}
DD = evalin('base','ParamDD');
if isempty(DD)
disp('Parameter of DataDictionary is empty.');
else
DataSecObj = getSection(DD, 'Design Data');
DdVarList = find(DataSecObj);
DdFile = evalin('base','DdFile');
idx_DataSource = strcmp({DdVarList(:).DataSource}, DdFile);
DdVarNameAll = {DdVarList(:).Name};
DdVarName = DdVarNameAll(idx_DataSource);
num_iter = length(DdVarName);
paramName = parameters{1}; % ParameterName?
param2 = parameters{2}; % newValue?
param3 = parameters{3}; % newDatatype?
param4 = parameters{4}; % ??
for idx_var = 1:num_iter
VarPropEntry = getEntry(DataSecObj, DdVarName{idx_var});
value = getValue(VarPropEntry); % value is of type Simulink.Parameter
if ~isa(value, 'Simulink.Parameter')
continue;
end
if VarPropEntry.DataSource ~= DdFile
continue;
end
if strcmp(paramName, VarPropEntry.Name)
value.Value = param2;
% if(strcmp(param4, 'null'))
% value.param4Property = '';
% else
% value.param4Property = param4
% end
if ~isempty(param3)
value.DataType = param3;
end
% Use setValue to write the modified value back to SLDD
setValue(VarPropEntry,value)
% SaveChanges
saveChanges(DD);
break;
end
end
end
end
Additionally, I would suggest that you provide the SLDD object, and filenames as input arguments to the function instead of performing “evalin” on the base workspace. Ideally you can rework the code to break the entire for loop and query for only the SLDD data related to "parameters{1}".

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by