Whay not working saveChanges(DD) in mscript?

9 views (last 30 days)
sua
sua on 14 Aug 2023
Answered: Githin George on 4 Dec 2024 at 9:31
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 Comment
Yash
Yash on 29 Aug 2024
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.

Sign in to comment.

Answers (1)

Githin George
Githin George on 4 Dec 2024 at 9:31
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}".

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!