Are in a Masked subsystem the instance-specific changes saved with model save?

5 visualizaciones (últimos 30 días)
According to Introduction to Mask Initialization and Parameter Callback Code - MATLAB & Simulink and the given "Rules for Mask Initialization Code"
  • Avoid unconditionally modifying the contents of a subsystem from the mask initialization code. Such unconditional modification does not work properly when using model reference.
I am trying to skip the initialization if it was done once.
Since i have issues, my question is:
  • Are in a Masked subsystem the instance-specific changes saved with model save?
  • So is a init once and then save it ok?
  • If so, i think there are bugs at saveing. not all details are safed and then they are missing the next time opening.

Respuestas (2)

Aabha
Aabha el 13 de Jun. de 2025
Changes made by mask initialization code can be saved with the model, but only if they affect block parameters that are not dynamically created or altered at runtime and are model-reference compatible. But in some cases, instance-specific or dynamically created changes (like adding ports, changing internal block structure, etc.) might not persist reliably across all instances especially in the following situations
  • If you are using model references.
  • If you don’t trigger reinitialization.
  • If you use temporary run-time code inside the mask initialization.
If your initialization code conditionally executes, Simulink may not re-run that code during model load, or re-run it in unexpected ways (e.g., multiple times in nested subsystems). If you notice that some saved details are missing after reopening the model, it might be due to modifications inside the mask initialization that affect internal block parameters, connections or ports, or if the initialization code depends on state, but that state is not persistent across sessions.
As an alternative, if you want to do one-time initialization and persist the changes across sessions, you can use a ‘mask parameter’ to conditionally trigger callbacks linked to it. Parameter callbacks only run when the corresponding parameter changes, so you will be able to control when the callback code runs based on the value of the parameter, as mentioned in the guidelines for ‘Authoring Parameter Callback Code’ in the following link: https://www.mathworks.com/help/releases/r2025a/simulink/ug/initialize-mask.html#mw_97b4e021-7df1-430b-885b-ce2f72ee87d8
Please refer to the following documentation link for more information about ‘Mask Parameters’ and ‘Callbacks’: https://www.mathworks.com/help/releases/r2025a/simulink/ug/author-mask-init-callback.html
I hope this answers your question.

Samar
Samar el 13 de Jun. de 2025
Hello Sebastian,
Instance-specific changes to a masked subsystem—such as parameter values configured via the dialog—are indeed saved with the model. When subsystem is masked, Simulink stores that mask’s workspace (the values of its parameters) inside the model file, and these values persist across saves and reloads. However, there are some cautions:
  1. Unconditional structural changes—like always adding or deleting blocks—are not supported with model referencing. MathWorks warns against this scenario—especially when using model reference—and behaviour can become unpredictable. It is documented here: www.mathworks.com/help/simulink/ug/initialize-mask.html#mw_e3cddfc4-0387-446d-9d52-51ab55133dbc
  2. Mask initialization code execution timing—when you load or restore a model, Simulink may or may not re-run the mask initialization code depending on whether the mask workspace is flagged as “up to date.” That’s an optimization to avoid unnecessary reinitialization. It is documented here: www.mathworks.com/help/simulink/ug/initialize-mask.html#mw_9dd8ac3c-25e4-4e55-a68c-9b963d5f0d31
If you are seeing missing data after reopening, this usually stems from:
  • Using structural changes in your init code that are not properly selfmodifiable (or supported) under model referencing.
  • The init code doesn't execute on load because Simulink already sees the mask workspace as unchanged, so new settings are not applied.
  • Attempting to clear or override existing mask variables or workspace contents, which is discouraged.
The documentation for these can be found here:
Recommended Solutions:
  1. If one-time structural change is needed: Use a selfmodifiable masked subsystem. Enable “Allow mask initialization code to modify the subsystem’s content” in the Mask Editor. You can find more info here: www.mathworks.com/help/simulink/ug/mask-subsystem-file.html#mw_8167ed7c-7b67-4e89-9843-ed1a13805bf5
  2. To avoid rerunning init logic on every load: In your mask init code, wrap structural or heavy changes within a guard condition, e.g.:
Code
if ~isfield(get_param(gcb,'UserData'),'isInitialized')
... do init ...
ud = get_param(gcb,'UserData');
ud.isInitialized = true;
set_param(gcb,'UserData',ud);
end
3. If mask data seems lost: Avoid using clear inside init. Instead rely on stored mask variables or persist state via UserData if needed.
This pattern will give you the behaviour you are aiming for—initialize once, save it, and have Simulink reliably retain it through model saves, loads, and references.
Regards,
Samar

Categorías

Más información sobre Subsystems en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by