variables with scope local to subsystem

12 visualizzazioni (ultimi 30 giorni)
Matthew Mishrikey
Matthew Mishrikey il 10 Mar 2022
Risposto: Gyan Vaibhav il 9 Nov 2023
I'm a bit confused about variable scope. I think I know what's going on but am looking for confirmation and maybe advice on the correct way to handle things.
Presently my subsystem uses base workspace variables. I thought that would break when I duplicate the subsystem and the base workspace values get clobbered, but it works okay.
I think what happens is that one subsystem is initialized at a time. Its initfcn callback sets the base workspace variable for itself. Then when the next subsystem is initialized, the base workspace variables are changed. After the sim, the base workspace variables are set to those of the last initialized block.
Is there a way to have duplicate variables scoped to each subsystem?

Risposte (1)

Gyan Vaibhav
Gyan Vaibhav il 9 Nov 2023
Hi Matthew,
I understand that you want to understand the scope of variables you are using for your subsystem which are defined in the initialization function of the models. Further, you want to know if there is any way to define variables that have scope to the subsystem or model.
Yes, the understanding of the base workspace variable behaviour is correct. When multiple subsystems are initialized, each one may set the base workspace variables during its initialization. The final state of these variables will reflect the last subsystem that was initialized.
However, using base workspace variables in this way can lead to confusion and is generally not recommended because it's hard to track the changes and the scope is global, which could lead to unexpected behaviour.
To have duplicate variables scoped to each subsystem, Masks or the Data Store Memory blocks can be used.
Here's an example of how to use and create Mask for the above stated purpose:
  1. Right click on your subsystem.
  2. Select Masks and then Create Mask.
  3. In the Parameters & Dialog tab, you can add “Edit” parameter for all the local variables.
  4. Now you can make copies of this masked subsystem and double click on it to set the values of the variables.
You can refer to the following documentation pages for more insight on “Masks” and the “Data Store Memory Block”:
Hope this helps and resolves your issue.
Thanks
Gyan

Categorie

Scopri di più su Subsystems in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by