Inconsistent Variable Naming in 'To Workspace' Blocks with Multiple Subsystem Reference Instances

조회 수: 2 (최근 30일)
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!

답변 (2개)

Zinea
Zinea 2024년 9월 30일
Hi Javier,
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!

Javier
Javier 2024년 10월 1일
Thank you for the responses. Option 3 seems like the most direct approach to the problem. I have tried it, and the renaming works correctly for the first subsystem; however, for the second one, it says it is blocked by the first. I have tried saving the model after each renaming, but it still gives the same error.
I will explore your ideas to see if I can find a solution.

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by