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

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

Related

Simulink code generation: set tunable parameters programmatically

I use Simulink Coder to generate code from a huge model. I use a rsim target with tunable parameters to be able to give the executable variable inputs via a parameter file.
I can specify which model parameters should be tunable (by default all parameters will be in-lined in generated code on compile time) via the code generation settings:
code generation options > optimization > signals and parameters > configure default parameter behavior
Here I can manually choose from all workspace variables the ones I want to be globally tunable:
Q: Is there a way to add a variable (given its name) to this list programmatically?
For example if a have list of 50 variables i would want to add them to the tunables list, via a MATLAB script without having to add every single one manually. Or loop over the list and set the tunable setting on each one.
I can generate a parameter struct which contains a list of tunable parameters using rsimgetrtp('model_name'). But I was not able to find a function to actually set the parameters in the first place.
I use Matlab 2015b for this, since its legacy code.

Simulink: replace existing subsystem with another one

How do i replace an existing SubSystem in a given model with another SubSystem from another model.
I tried commands like:
Simulink.SubSystem.copyContentsToBlockDiagram(subsys, bdiag)
replace_block
but nothing worked (maybe I used it wrong).
Remarks:
I am using matlab 2011b.

Simulink: Control Variant Subsystems Using Mask

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.

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)