Is there a way to programmatically list the variables in a simulink .slx model without having the model in a "runnable" state
13 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Animesh
2024-9-17
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:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Programmatic Model Editing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!