Clear Filters
Clear Filters

How to configure set_param() per instance for arrays

7 views (last 30 days)
I have a function 'deep' within a model that is called as an array. Each instance of this function needs a different value for its parameter. I already found that you can set the parameter with the set_param() command. This worked fine as long as I only had 1 instance in my array. But I need multiple. How can I set each instance its own value?
In more details. I have an array of 'DEV_DspBox' which receives its data from a PLC.
Setting a single value for the parameter works with this:
set_param('PlayGround_Model/DEV_DspBox[n]/DEV_DspBox/DEV_DspProbe_A/DEV_DspProbeSensorLeft/fbTriggerBuffer', 'nParNrOfDataRecords', "5");
But I need to be able to set the 'nParNrOfDataRecords' of the fbTriggerBuffer for each instance of DEV_DspBox separate. The value is depending on a distance of the sensor to a reference point. Each 'probe' is located within a different hardware position, so the buffer size is depending on the distance of the 'probe' to the reference point. Causing that the parameter needs to be different for each instance. How to do this?
Thanks for any suggestions in advance.
Cheers
Ludo
  2 Comments
madhan ravi
madhan ravi on 19 Dec 2023
Ludo could you please illustrate with an example? Since we don’t see the model complete , it’s hard to follow. When you mean single value, what do you mean? You want to make it a vector instead of a scalar? With your code you set nParNrOfDataRecords 5 but you want for example 5, 10? Is that right?
Ludo Houben
Ludo Houben on 19 Dec 2023
Hi Madhan Ravi
Here I graphical example of my request. I have a software block 'probe' (right side of image) that contains a software block for a 'sensor' which has a software block for storing multiple results. The amount of storage space is depending on the distance to a reference point (left side of the image). The amount of storage spaces is a parameter on the 'store' software block. I call the 'probes' in an array, as I need 4x the same software block. But each instance needs a different value for the storage space. In this case needs probe 1 (gray) 7 spaces, probe 2 (green) 5 spaces, probe 3 (red) 3 spaces and probe 4 (blue) only 1 storage space. The values need to be configurable for each instance, as their position can be changed. So on the 'highest' level (see earlier picture) I need to be able to adjust the values. Creating the values is known, but I don't know how to reference each parameter of each instance.
Regards
Ludo

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 19 Dec 2023
Edited: Fangjun Jiang on 19 Dec 2023
This is "For Each Subsystem". You need to know how to use it. See the document and examples.
set_param() is not the correct way to do it. Instead, you give that block parameter a variable name, say, MyParam. Usually, you set MyParam=5 in workspace and then it is all set.
When using "For Each Subsystem" with N=5 for example, you set MyParam=[1 2 3 4 5]. Then MyParam=1 will be passed to the subsystem when N=1, MyParam=5 will be passed to the subsystem when N=5. This is called parameter partition.
See the double loop icon of the Inport and Outport block? That means that signal is "partitioned".
Double click that "For Each" block will show you how to "partition" signal and parameter.
  5 Comments
Fangjun Jiang
Fangjun Jiang on 19 Dec 2023
By which block is this parameter used?
Anyway, Click that "Working with For Each Subsystems" link on the "ForEach" dialog window, it will bring up the document and example. You will need to follow the section of "Model Parameter Variation Without Changing Model Structure".
Ludo Houben
Ludo Houben on 3 Jan 2024
Hi Fangjun Jiang
Sorry that it took a while, I now managed. Thank you :-D
Regards
Ludo
For others also struggling here are my steps:
  1. Create a 'property' within your function. It should be 'tunable'.
  2. Go to the calling subsystem and create a 'Mask' (if not already done)
  3. Add a variable with 'Parameter\Edit', give this variable a good name. In the example 1, 'nParNrOfDataRecord', see blue circle in mask editor.
  4. Couple this variable to the parameter of the function/subsystem that you are referencing too. In the example 1, the parameter '[] Number of data records:'.
  5. Repeat steps 2->4 for each 'higher' level subsystem which is calling a subsystem that uses the function (See example 2).
  6. On the 'highest' level you then connect the array as mentioned by Fangjun Jiang. In the example 3, these are the 'arrnParNrOfDataRecords....' variables within the blue circles.
  7. The variables that are created in the Mask that calls the 'for each' subsystem will show in the 'Parameter Partition' tab in the 'For Each' configuration. Here you can devide your variable as done with the in-/outputs. Don't forget to couple the variables to the subsystem that you are calling. See example 4.
Example 1:
Example 2:
Example 3:
Example 4:

Sign in to comment.

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!