How can I change the number of ports for VariantSource programmatic?

1 vue (au cours des 30 derniers jours)
Martin
Martin le 1 Fév 2017
Modifié(e) : Ayush le 14 Mai 2025
I'd like to change the number of ports for the VariantSource block. If I try to change the 'Ports' element with set_param command, I get the answer that this field is read-only. Has anyone an idea how I can change the number of ports programmatic?

Réponses (2)

Anudeep Kumar
Anudeep Kumar le 22 Avr 2025
Hey Martin,
I looked into the "blockHandle" for the "Variant Source" block and found that the "VariantControls" property is the correct one to modify when you want to change the number of ports.
The "VariantControls" property is a cell array, as shown below:
>> get_param(gcb,'VariantControls')
ans =
2×1 cell array
{'true' }
{'false'}
To add a new port you can do the following:
a= get_param(gcb,'VariantControls')
a{3}='true' % Use 'true' or 'false' based on your requirement
set_param(gcb,'VariantControls',a);
Simply add to the cell array returned by “get_param” as needed and then set it using “set_param.”
Hope this helps!

Ayush
Ayush le 14 Mai 2025
Modifié(e) : Ayush le 14 Mai 2025
I understand you need to change the number of ports for the "VariantSource" block and "set_param" is a read-only field and cannot be edited.
To programmatically change the number of input ports of a "Variant Source" block, you need to modify the "VariantControls" property associated with each input.
The "VariantControls" property is a cell array and here is a pseudo MATLAB code for depicting the use for the same:
% Define the model and block name as modelName, blockPath respectively and
% load the model
% You can set the control mode to expression-based.
set_param(blockPath, 'VariantControlMode', 'expression');
% You can assign 2 variant conditions (creates 2 input ports).
set_param(blockPath, 'VariantChoices', '{V==1, V==2}');
% You can now define the variant control variable in the base workspace.
assignin('base', 'V', 1); % This activates the first input port
% You can now update the model to see the changes.
You can read more about "VariantControls" property here: https://www.mathworks.com/help/simulink/var/introduction-to-variant-controls.html
Hope it helps!

Catégories

En savoir plus sur Modeling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by