Inconsistent Variable Naming in 'To Workspace' Blocks with Multiple Subsystem Reference Instances
4 views (last 30 days)
Show older comments
Hello everyone,
I'm encountering an issue with a Simulink model that contains multiple Subsystem Reference blocks ("Subsystem Reference 1", "Subsystem Reference 2", etc.), all pointing to the same subsystem.
Inside this referenced subsystem, I have several "To Workspace" blocks, and their variable names are automatically numbered based on the Subsystem Reference they belong to (e.g., for Subsystem Reference 1, the block names are "out.A1", "out.B1", etc., and for Subsystem Reference 2, they are "out.A2", "out.B2", etc.). This numbering works perfectly and automatically when I copy and paste each subsystem reference.
However, when I close and reopen the main model, the numbering of the "To Workspace" blocks inside the Subsystem Reference changes unexpectedly. For example, after reopening, Subsystem Reference 1 might now have block names like "out.A3", "out.B3", etc., instead of "out.A1", "out.B1", which causes inconsistencies in the workspace variables. It seems that Simulink renumbers the variables each time the model is loaded.
Has anyone experienced similar issues or know of a way to prevent this renumbering from happening upon reopening the model? Any help would be greatly appreciated!
Thank you!
0 Comments
Answers (2)
Zinea
on 30 Sep 2024
This behaviour of renumbering of the blocks occurs due to how Simulink manages unique identifiers for blocks when the model is loaded. You may use the following strategies to prevent it:
1.Instead of relying on automatic numbering, the variable names in the “To Workspace” blocks can be manually set to prevent renumbering.
2. Model callbacks (such as “PreLoadFcn” or “PostLoadFcn”) can be used to programmatically set the variable names of the “To Workspace” blocks when the model is loaded. You may refer to the following link to know more about model callbacks: https://www.mathworks.com/help/simulink/ug/model-callbacks.html
% Set this code in the model's PreLoadFcn callback
blocks = find_system(bdroot, 'BlockType', 'ToWorkspace');
for i = 1:length(blocks)
set_param(blocks{i}, 'VariableName', ['output_' num2str(i)]);
end
This script should be manually run after loading the model to set the variable names.
3. A custom MATLAB script can be created that runs each time the model is loaded. This script should iterate through the ‘Subsystem reference blocks’ and set the variable names for the “To Workspace” blocks according to the desired naming convention.
% Custom script to set variable names for "To Workspace" blocks
model = 'your_model_name';
load_system(model);
blocks = find_system(model, 'BlockType', 'ToWorkspace');
for i = 1:length(blocks)
set_param(blocks{i}, 'VariableName', ['output_' num2str(i)]);
end
This code is automatically executed every time the model is loaded, ensuring variable names are set consistently without manual intervention.
4. A ‘Simulink Data Dictionary’ can be used to manage and store variable names centrally, thus maintaining consistency across model loads. You may refer to the following link for more information:
Hope this helps, cheers!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!