Scope and hierarchy of variables in Simulink

4 views (last 30 days)
Is there any documentation on variable scope in Simulink. My understanding is that lower level units like masked subsystems have visibility of what I would regard as higher level areas like the model workspace, but the insides of masked subsystems are not visible to global models.
I'm struggling with this a bit because in most programming languages re-usable sub-units have limited scope. For example, a Matlab function would not be able to see the workspace of the calling function, or the Matlab workspace for that matter. On the other hand, a masked Simulink subsystem can see the Simulink workspace.
In the case of conflicts, what takes precidence? For example if I name a mask parameter VAR1 and I also have a variable in the Simulink workspace called VAR1 and a data dictionary entry called VAR1, and a Matlab workspace VAR1, which value is used? (This is the reason I'm asking the question.)
Is there a way to limit the scope of a masked subsystem? For example I see that it is possible to restrict a Simulink model such that it cannot access the Matlab workspace. I can't see similar functionality for masks.
Sorry if this is all obvious. Not easy to search the internet when most hits are related to Simulink scope blocks, ie for viewing signals.

Accepted Answer

Divyam
Divyam on 11 Sep 2024
Hi @men8th,
In case of a conflict between variables of the same name, Simulink follows the following order of precedence based on its search path:
  1. Mask Parameters
  2. Model Workspace
  3. Data Dictionary
  4. MATLAB Base Workspace
The scope for various Simulink model variables can be limited by the following methods:
  • Ensuring that variables in different scopes have unique names to avoid conflicts.
  • Using data dictionaries to manage variable definitions and ensure consistent usage across models.
  • Using initialization code in the mask to define local variables.
  • Using the "Permit Hierarchical Resolution" option of any subsystem you can choose to search up the hierarchy of a subsystem if the search reaches that subsystem without resolving to a workspace variable.
  • Configuring explicit symbol resolution such that resolution can be specified based on the object type.
For more information regarding limiting the scope of variables using Signal resolution, refer to this documentation: https://www.mathworks.com/help/simulink/gui/signal-resolution.html

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!