Clear Filters
Clear Filters

setdiff on Simulink VariableUseage

3 views (last 30 days)
Reuben Symons
Reuben Symons on 9 Feb 2022
Answered: Dinesh on 29 Dec 2023
My goal is to identify data dictionary definitions defined in the datadictionary not defined in the model. Using the search function doesn't work as they are referenced data dictionaries.
I have created two variableUseage objects, one from the model and one from the datadictionary I am trying to find unused variables in.
I can see that the two variableUseage objects contain identical entries save for the users paramater which is empty in the one created from the datadictionary. The reference for setdiff states:
setdiff compares the Name, Source, and SourceType properties of the Simulink.VariableUsage objects in VarsIn1 with the same properties of the objects in VarsIn2. If VarsIn1 and VarsIn2 each contain a Simulink.VariableUsage object with the same values for these three properties, the objects describe the same variable, and setdiff does not return an object to describe it.
When I run setdiff I get the entire contents of VarsIn1 back despite manually confirming that most of the variables in varsin1 exist in varsin2 with identical Name, source and SourceType.
function deleteUnusedDD(model)
% model is a string handle for the open model to perform this on.
ddInternal = Simulink.data.dictionary.open(strcat(model, '_internal.sldd'));
iDDVars = getSection(ddInternal,'Design Data');
innerVars = find(iDDVars, '-regexp','DataSource', strcat(model, '_internal.sldd'));
innerUseage = Simulink.VariableUsage({innerVars.Name},strcat(model, '_internal.sldd'));
% %load the model vars
load_system(model);
modelVars = Simulink.findVars(model,'SearchReferencedModels','on','SourceType','data dictionary'); % has 300 vars of which 150 are in innerUseage
toDelete = setdiff(innerUseage,modelVars); % has 154 vars of which 150 are in modelvars
% I expect toDelete to contain 4 vars.

Answers (1)

Dinesh
Dinesh on 29 Dec 2023
Hi Reuben,
It seems you're facing an issue with the "setdiff" function when comparing "Simulink.VariableUsage" objects. The function should ideally return variables that are in "VarsIn1" but not in "VarsIn2," based on the "Name," "Source," and "SourceType" properties. If "setdiff" is returning all variables from "VarsIn1," it suggests that it's not finding a match in "VarsIn2" for those variables, even though you have manually confirmed that they should match.
This could be due to several reasons, such as small differences in the properties of the objects that aren't properly visible or an issue with the data type or format of the properties.
To debug this, I believe that you have to manually check and compare the properties of a few "Simulink.VariableUsage" objects from both "VarsIn1" and "VarsIn2" to confirm they are exactly the same.

Categories

Find more on Manage Design Data in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!