Simulink: Control Variant Subsystems Using Mask - matlab

I would like to find a simple process for switching the model innards under a mask using the mask parameters.
This question has expanded enough such that it has been reimplemented here.
Variant subsystems are an excellent method and can be controlled via workspace parameters;
however, I have found mask parameters to not interface with the variant subsystem selection.
This link is the first of a series of posts on how to use mask parameters to make changes to blocks inside of the system;
however, the method is not as intuitive as using variant subsystems and a switch.
The link is also from 2008 and I believe that it may have been superceded at some point.
MWE
I have made a model containing a system labeled Source.
It is connected to a Display block which displays its output.
Source is a variant subsystem.
It contains 3 variants:
Source\One
Source\Two
Source\Three.
Each variant contains one Constant block.
The value of the Constant block is eponymous with the block label.
For example, Source\Two contains a constant block with value 2.
Source is also a masked subsystem.
Its mask contains a Radio Button parameter with a value labeled variantValue.
The Radio Button options for the variantValue parameter are:
Choice 1
Choice 2
Choice 3
The mask Initialization code is as follows:
switch variantValue
case 'Choice 1'
set_param('Source','OverrideUsingVariant','One')
disp('One')
case 'Choice 2'
set_param('Source','OverrideUsingVariant','Two')
case 'Choice 3'
set_param('Source','OverrideUsingVariant','Three')
end
I have set the variant to Override.
I cannot set the mask to allow library blocks to modify contents, as this is greyed out.
I will drop the variant subsystem deeper into the hierarchy from the masked subsystem when a masked subsystem which is a variant subsystem works.

To do anything which does not go via your base workspace, you first need to set the "Overwrite variant conditions...", now you can choose the active variant with code:
set_param('untitled/Variant Subsystem','OverrideUsingVariant','Variant1')
What remains is creating a mask which, whenever the parameter is changed in your mask, runs the above line. This can be done via the initialisation commands.

Related

How to use structure as parmeter for simulink subsystem and use fields inside as block parameters?

I have created a subsystem in Simulink. I would like this subsystem to have a single parameter, which is supposed to be a Matlab structure. Inside the subsystem, I would then like parameters of some of the blocks to be the fields of this structure.
So for example, imagine the subsystem has a single parmeter sys_inputs, then inside the subsystem I have two Constant blocks and the value for the first Constant block should be sys_inputs.Constant1, and the other should be sys_inputs.Constant2.
Is this possible, and if so, how exactly?
I'm finding the Matlab documentation on passing mask parameters to subsystem internal blocks a bit obtuse.
The purpose is so that the user only has to provide a single parameter to the subsystem instead of changing many parameters, when this input usually comes pre-packaged as a structure.
I discovered the answer myself.
First create the subsystem
Right click and 'create mask'
Edit mask parameters
Add an 'edit' parameter. The 'Name' of this parameter will be the variable name you can use inside your subsystem, e.g. sys_inputs
Inside the subsystem, you can use the sys_inputs parameter as though it was a workspace variable, so you can enter things like sys_inputs.Constant1 in the subsystem components parameters

How to select output in Anylogic by agent parameter?

So, I'm pretty new with Anylogic, but have done a lot of tutorials and I have programming experience in Java. For my thesis I'm modelling a vehicle flow as a process. In the source block, I create custom agents (vehicles) with some parameters from the database. This works fine. Then I want to assign an electric parameter with randomTrue(0.5). For this, I call a setupTaxi-function, where electric ist set. The parameter for the randomTrue-function should be changeable, so I set it as an extra paramter anteilEtaxis (0.5).
After that, I want the vehicles to do different things depending on the value of electric using SelectOutput. I selected the Condition and test on agent.electric.
I basically did the exact same thing as described in the Anylogic help. And yet the framework always chooses the true Output port, no matter if the parameter ist set to true or false.
See the image for setup and parameters. I tested this via console (the first line is a println-call in source, the second a println-call in selectOutput.). Plus you can see that the parameter is set to different values, because the 3D visualisation model depends on it:
enter image description here
Also, I tried a few different combinations of setting the parameters, reading them etc... The only thing that will work is putting randomTrue(0.5) directly in the Condition box. This is not what I want though. So if you have an idea, what is wrong, please tell me.
This is a typical beginners problem.
I will assume you are calling the setupTaxi-function in your source in the "on exit" action... If you are doing that, then it's too late and the agent already made its decision on where it will go after the select output block.
You have to call your setupTaxi-function in 2 possible places:
1) In your source on the "on at exit" action
2) In your vehicle agent on the "on startup" action
Or even.. just make electric variable have a default value of randomTrue(main.anteilEtaxis)... that will also work.

Simulink & Masks: Dynamic access to parameters "evaluate" and "tunable"

First of all, matlab version is 2011b, so I cannot use Simulink.MaskParameters class.
I have one simulink mask and some parameters inside. I need to determine in my function for each parameter if it is "evaluable" or "tunable".
Those two things are two checkboxes in the mask parameters dialog which you can select for any parameter.
For "tunable", there's the MaskTunableValues property. For "enable", there's the "MaskEnables" property.
Do you know if there's a way to programmatically access the same property but for "evaluate" ?
Thanks
#Phil Goddard's answer shows you how to find the parameter. To complete the answer, the actual parameter is MaskVariables. Evaluate flag is embedded into the MaskVariables string. It is not straightforward to modify this. For example for two parameters MaskVariables string contains something like this:
"a=#1;b=&2;"
In this string the # sign indicates evaluation. Based on that, parameter a is evaluated and parameter b is not. If you want to change the evaluate flag, you need to set this string for MaskVariables parameter exactly how it was except for # or & signs.
For more information see R2011b documentation for mask parameters at https://www.mathworks.com/help/releases/R2011b/toolbox/simulink/slref/f23-18517.html. Towards the bottom of the page there is more detail about MaskVariables parameter.
You're using too old a release for most people to be able to give you an exact solution, however, I'm sure (from memory) that this parameter is available.
If you click on the mask to select the block, then go to the MATLAB command line and type
get_param(gcb,'ObjectParameters')
you'll get a list of all the block properties. (You may already know that since you're aware of MaskTunableValues and MaskEnables.) Near the bottom of that list are all of the properties related to the mask.
Now manually look at each/all of those properties, e.g.
get_param(gcb,'MaskTunableValues')
and you'll find that one of them is a structure that contains the information that you're looking for. (You may need to dig down into the structure to find the specific info.)
Answer for versions > 2011b (tested on 2014b):
Ok found it, actually the matlab documentation is REALLY unclear regarding the Simulink.MaskParameter class and here is how it works:
First, get the Mask class from your block:
mask = Simulink.Mask.get(gcb)
The Mask class is a structure containing all mask parameters:
parameters = mask.Parameters(:)
parameters is a (array of) Simulink.MaskParameter object which will contains all the necessary properties, including Evaluate.

In simulink, programmatically create a variant subsystem

I want to use scripting to create and define a variant subsystem in simulink.
I am able to create the variant subsystem and add subsystems within it using the add_block but cannot find the appropriate property that defines the variant subsystem by using the get_param command.
The way I graphically configure a variant subsystem is by right clicking the block and going to Block Parameters and then adding the variant control conditions for each subsystem.
I want to do the same thing but from an m-file so that I can create multiple blocks programmatically.
I checked the documentation and google search but couldn't find anything.
Thanks in advance.
Based on a comment by #Praetorian, I opened the model in a text editor and found the parameter line where the variant condition was defined.
I found that the variant conditions are stored in the subsystems within the variant subsystem and not within the top-level variant subsystem.
So you store the condition within each subsystem's "VariantControl" parameter.
Example:
set_param([variantSys '/' sys],'VariantControl','a==1') where sys is a subsystem within the variant subsystem variantSys.
The variant control should be written on the Callbacks of the Model. In order to put the simulink variant controls available to the model, the variables should be created in the workspace. For this to happen you need to put your Sys = Simulink.Variant(Mode== value) etc on the PreLoadFcn.
Check access PreLoadFcn and put the formulas in there then your table will be automatically filled.
If you do not mind can you tell me how you created the variant subsystem?
Good luck

How to use base workspace variables while initializing the masked block in MATLAB?

I have a structure called "uwb" in the base workspace. Under the uwb structure I have another structure called "channel". Under channel I have got two variables, a and b. Now I want to create a subsystem. I want to mask the block. My problem is I have to use the variables a and b for the initialization of the masked subsystem. How can I include a and b in initialization commands of the subsystem while masking?
After creating a mask for the subsystem:
Select the Parameters tab in the subsystem mask editor to add tunable dialog parameters.
Add the dialog parameters for each variable you need to access (i.e. call them maska and maskb).
Head over to the Initialization tab and add your initialization code referring to the dialog parameter names maska and maskb. Apply your changes close the mask editor window.
Double click on the masked subsystem and you should be prompted to enter values for the two dialog parameters that were just setup.
In the textfields, type in the workspace variables uwb.channel.a and uwb.channel.b to assign their values to maska and maskb respectively.
As long as the uwb struct is in the base workspace when the model is initialized to run, the masked subsystem will evaluate and assign the a and b appropriately.
(I just tried it out and it seems to work fine, here is the model as a reference: http://sfwn.in/Fejp)