Is there a way to programmatically list the variables in a simulink .slx model without having the model in a "runnable" state

12 vues (au cours des 30 derniers jours)
So the general workflow to list all of the variables in a simulink file is with Simulink.findVars. The problem is that this requires the model to be in a "runnable" state, or at least able to perform a diagram update. This makes it inconvenient to make simulation tools because it requires all of the variables to be initialized before you can find them. In my workflow it would be more convenient to list the variables and THEN initialize them, now that I know what the variables are.
model = "MyModel";
try
%Running with cached, doesn't really help because the cache isn't
%persistent between matlab sessions (ie if I restart my computer I need
%to re-compile the model anyway)
Variables = Simulink.findVars(model,'SearchReferencedModels','on','SearchMethod','cached');
catch ME
%I add the warning here because it is very likely the user will
%attempt to run this function BEFORE initializing every variable... my
%only workaround right now is to have loaded a file with every
%variable pre-defined.
warning("Simulink Requires a Properly Initialized Model to analyze inputs, please ensure the model will run by loading default values for each calibration, constant, and input")
error(ME.message)
end

Réponses (1)

Animesh
Animesh le 17 Sep 2024
Finding uninitialized variables using "Simulink.findVars" might not be feasible. However, a workaround involves using a combination of the "find_system" and "get_param" functions. This method allows you to find all the blocks and loop through each block and subsystem to check for parameters that are likely variable names.
Here is a sample code snippet for reference:
load_system('test_model');
% Find all blocks
blocks = find_system('test_model', 'Type', 'Block');
variableList = {};
for i = 1:length(blocks)
% Get block parameters
params = get_param(blocks{i}, 'DialogParameters');
paramNames = fieldnames(params);
for j = 1:length(paramNames)
paramValue = get_param(blocks{i}, paramNames{j});
if ischar(paramValue) && isvarname(paramValue)
variableList{end+1} = paramValue;
end
end
end
disp('Potential Variables:');
disp(unique(variableList));
close_system('test_model', 0);
However, note that this approach may list all parameters, not just those requiring initialization. You may need to modify the code according to your model to filter out unnecessary variables.
You can refer the following documentations for more information:

Catégories

En savoir plus sur Programmatic Model Editing dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by