How can I calculate a mask parameter based on another mask parameter?

I am creating a mask for an electric load. I want the user to be able to specify the minimum voltage for the load. The mask asks the user to enter a value for the load's rated voltage in volts (Vrated) and a value for the minimum voltage as a percent (Vmin_p). I would like to have a third parameter for the minimum voltage in volts (Vmin) which is automatically calculated as Vrated * Vmin_p / 100.
For example, if Vrated is 120 and Vmin_p is 90, a third parameter (Vmin) should be created and calculated as 120 * 90 /100 or 108.
How should I go about this? Should I use set_param in the mask Code section?

Réponses (1)

akshatsood
akshatsood le 13 Sep 2023
Modifié(e) : akshatsood le 14 Sep 2023
Hi Dajan,
I understand that you want to calculate a mask parameter based on another mask parameter. This can be achieved by leveraging the methods described in the Simulink.Mask class. To illustrate my approach, I have assumed a example model named sample_model that contains a subsystem with a mask applied to it
To clarify my approach, I utilized the Simulink.Mask.get method to extract the handle to the mask from the model. By using get_param, I retrieved the values of the mask parameters (Vrated and Vmin_p in your case). With these values, I calculated Vmin and leveraged the addParameter method to create the parameter and assign it the computed value.
Here is the code snippet that demonstrates the approach
path = 'sample_model/mask';
maskObj = Simulink.Mask.get(path); % get mask handle
param_one = get_param(path, 'value@Vrated'); % retrieve value for Vrated
param_two = get_param(path, 'value@Vmin_p'); % retrieve value for Vmin_p
Vmin = num2str((param_one * param_two)/100);
% create and add the mask parameter
maskObj.addParameter('Name', 'Minimum_Voltage', 'Prompt', 'Vmin', 'Value', Vmin);
Have a look at the below references for better understanding
I hope this helps.

3 commentaires

Thanks for the reply, but I'm not sure where I should write the code. I tried writing in the initialization section, but I keep getting an error.
% Initialization code section
function initialization()
path = 'Dynamic DC Load/mask';
maskObj = Simulink.Mask.get(path); % get mask handle
param_one = get_param(path, 'value@Vrated'); % retrieve value for Vrated
param_two = get_param(path, 'value@Vmin_p'); % retrieve value for Vmin_p
Vmin = num2str((param_one * param_two)/100);
% create and add the mask parameter
maskObj.addParameter('Name', 'Minimum Voltage', 'Prompt', 'Vmin', 'Value', Vmin);
end
% Parameter callback section
akshatsood
akshatsood le 14 Sep 2023
Modifié(e) : akshatsood le 14 Sep 2023
To address the error you encountered, it appears that the incorrect path was specified for the Simulink.Mask.get method. I recommend creating the MATLAB script in the same location as your model and injecting the code into it.
To clarify on the path I have used in the code, I started by creating a blank model called 'sample_model'. Inside this model, I added a subsystem block named 'mask' and applied the mask to it. Please ensure that you have followed a similar approach for your model.
Also do change Minimum Voltage to Minimum_Voltage while using the addParameter method as the former is not a valid name.
Hey akshatsood,
Thanks for the help. I ended up using a similar solution. I created Vmin as a parameter and used the code to change it automatically. Vmin and Vmax are disabled so the user can't change them directly.
% Initialization code section
function initialization()
path = 'Library/Dynamic DC Load';
Vrated = get_param(path, 'value@Vrated'); % Retrieve value for Vrated
Vmin_p = get_param(path, 'value@Vmin_p'); % Retrieve value for Vmin_p
Vmax_p = get_param(path, 'value@Vmax_p'); % Retrieve value for Vmin_p
Vmin = num2str((Vrated * Vmin_p)/100); %Calculate value for Vmin
Vmax = num2str((Vrated * Vmax_p)/100); %Calculate value for Vmax
set_param(path,"Vmin",Vmin); %Set value for Vmin
set_param(path,"Vmax",Vmax); %Set value for Vmax
end
Thanks!

Connectez-vous pour commenter.

Catégories

Produits

Version

R2023a

Question posée :

le 13 Sep 2023

Commenté :

le 17 Sep 2023

Community Treasure Hunt

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

Start Hunting!

Translated by