Struct as mask input

25 vues (au cours des 30 derniers jours)
Prestonr
Prestonr le 6 Jan 2024
Commenté : Prestonr le 16 Jan 2024
Hello, I would like to have a struct as a mask input to a Model Reference. I will have multiple identical model references in my model. When I currently try to modify the mask parameter to use a struct, I get the following error:
"Variable 'blah' has ben deleted from the base workspace."
The struct contains information for a large number of blocks and I am trying to have the blocks extract the information from the struct in the normal way (e.g., Block param: blah.field.block_param_value).
Everything works if I have the struct defined in the base workspace and then have the sub-model (used by the model reference) use that directly. However, this does not allow me to use multiple identical model references because they would all point to the same struct in the base workspace.
I have looked at a number of other posted questions asking about this issue and have read that this is possible but without in-depth information as to how.
The following asks this but the answer does not work for me-
This references it but was never answered-
Other resources suggest using a non-virtual bus, but I am unsure how that accomplishes this goal if I'd like to access the fields in a struct-like fashion.
Very much appreciate the help ahead of time :)
  4 commentaires
Paul
Paul le 6 Jan 2024
Is masking the Model block a requirement?
Prestonr
Prestonr le 6 Jan 2024
If it is possible to directly modify the workspace of a model reference block then that would suffice. However, I do not believe model reference blocks have their own workspaces.

Connectez-vous pour commenter.

Réponse acceptée

Paul
Paul le 6 Jan 2024
I did the following:
1) created a model child.slx. It has an Inport -> Gain -> Outport. The gain parameter is set to 'parameters.gain' (no quotes)
2) In the model workspace of child.slx, I created the Matlab variable 'parameters'. I this by creating the parameters struct in the base workspace, i.e., parameters.gain = 1, and then copied 'parameters' from the base workspace into the model workspace of child.slx. I couldn't figure out how to create a struct variable in the child.slx model workspace directlly.
3) In child.slx, I opened the Model Data Editor and checked the Argument box for 'parameters' .
4) Created model parent.slx. Added two model blocks, each set to reference child.slx.
5) In the first model block I set the Value field for 'parameters' under the Instance parameters tab to 'params1'.
6) In the second model block I set the Value field for 'parameters' under the Instance parameters tab to 'params2'.
7) In the base workspace, I created params1 and params2
params1.gain = 5;
params2.gain = 10;
8) Simuated the model. The gains of 5 and 10 flowed down to the Gain blocks in the child.slx models as expected.
I'm not too familiar with Simulink Parameters, so didn't try to make it work that way.
  1 commentaire
Prestonr
Prestonr le 16 Jan 2024
Apologies for not responding sooner.
As a status update I needed to use information from both @Paul and @madhan ravi's answer to get this to work. Although both helped, I am selecting @Paul as the accepted answer because he went through the trouble of working through this issue specifically for this instance.
Because I despise coming to these questions and never formally getting an update/detailed information on how it was solved, I am adding this in now :)
To do this I had to do the following:
Create a default struct with the same fields in the base workspace.
Turn it into a parameter (think "my_struct_param = Simulink.Parameter(my_struct); ")
my_struct_param = Simulink.Parameter(my_struct);
Add this parameter into the model workspace of child.slx.
child_model_workspace = get_param('child','ModelWorkspace');
assignin(child_model_workspace, 'my_struct_param', my_struct_param);
Go into child.slx, then open up Model Explorer. In there, you should find "my_struct_param". Check off the box that says "Argument"
While still in child.slx, right click anywhere and go to Mask->Create System Mask. "my_struct_param" should show up as a Parameter in here. Then click "Save Mask".
Now, in parent.slx, create a Model Reference to the child.slx. You should be able to double it and edit the parameter.
Note:
You cannot have strings as a value in the field or else this returns an error.

Connectez-vous pour commenter.

Plus de réponses (1)

madhan ravi
madhan ravi le 6 Jan 2024

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by