get a list of unused simulink variables of the matlab workspace in matlab

To get a list of unused Simulink variables in the MATLAB workspace, you can use the following code:

main.m
% get a list of all variables in the MATLAB workspace
vars = who;

% initialize an empty list to store unused Simulink variables
unused_vars = {};

% loop through each variable in the workspace
for i = 1:length(vars)
    % check if the variable is a Simulink parameter or signal object
    if isa(evalin('base', vars{i}), 'Simulink.Signal') || isa(evalin('base', vars{i}), 'Simulink.Parameter')
        % check if the variable is referenced in any Simulink model
        if evalin('base', sprintf('exist(''%s'', ''var'') && ~isempty(find_system(''Type'', ''block_diagram'', ''ReferenceBlock'', ''%s''))', vars{i}, vars{i}))
            % if the variable is referenced in a Simulink model, it is not unused
            continue;
        end
        % if the variable is not referenced in any Simulink model, add it to the list of unused variables
        unused_vars{end+1} = vars{i};
    end
end

% display a list of unused Simulink variables
fprintf('Unused Simulink variables:\n');
disp(unused_vars);
1003 chars
24 lines

This code will loop through all variables in the MATLAB workspace, and check if each variable is a Simulink parameter or signal object. If a variable is a Simulink object, it will check if it is referenced in any Simulink model. If a variable is not referenced, it will be added to the list of unused variables. Finally, the code will display a list of all unused Simulink variables.

gistlibby LogSnag