Reputation:
In this case I am trying to create a new block parameter as a new property to save a specific new data which I don't want to be saved in a defalut parameter which is already reserved for other diffrent related datas
for this parameter I want to use the command get_param and set_para that need to be alreday existed
I mean with default parameters , those . https://edoras.sdsu.edu/doc/matlab/toolbox/simulink/slref/parameters2.html#7515
Upvotes: 1
Views: 5059
Reputation: 2956
I'm not sure this is exactly what you are searching for, but I made an example on how to create programmatically a mask via script in MATLAB/Simulink. I will not use get_param
/set_param
, even if it is possible to obtain the same results using those commands. We will go with the Simulink
object that is simpler and more clear (at least IMHO).
For our playground lets create this simple subsystem (block
) with a simple constant that gives in output the name of a variable named a
that we want to take from the mask:
Look at the address of this block. My simulink model is mask.slx
, thus I can address this subgroup with the address mask/block
(upper left corner of the viewport), as you can see here:
At this point we can use the following code to add an edit parameter box for the subgroup, which fixes the value of a
:
clc
clear all
% The subgroup for which we want to programmatically create a mask
block_name = 'mask/block';
% Now we can create the mask parameter programmatically as you requested
% There are two way: the old one using get_param and set_param and a more
% clear one using the Simulink interface.
% I will go with thw second one, since it is really more straightforward
% with respect to the first one.
% The first think to do is to create the mask itself
% If the mask already exist, we would get an error, thus we can avoid it by
% checking if it already exist. This is something that you should check out.
mask_hdl = Simulink.Mask.create(block_name);
% mask_hdl = Simulink.Mask.get(block_name); % For use an existing mask
% Now we are ready to create the mask parameters:
edit_a_hdl = mask_hdl.addParameter( ...
'Type', 'edit', ...
'Prompt', 'Sets constant variable', ...
'Name', 'a');
edit_a_hdl.Value = '10';
Running the script the code will be masked and the variable will be set, as you can see here:
There are more information on this topic here.
Now lets say you have the playground as before done and you have the subgroup masked as in the last image. You can set its value in the mask programmatically (or get it) through the get_param
and set_param
as follows:
value = get_param(block_name, 'a');
value = str2double(value); % Values should always be string!
% Thus we must convert it
set_param(block_name, 'a', sprintf('%d', value * 100));
and as you can see the value has now been updated:
Again, you can achieve the same result by using the Simulink
object.
mask_hdl = Simulink.Mask.get(block_name);
edit_a_hdl = mask_hdl.Parameters(1); % We know its position in the struct array
value = str2double(edit_a_hdl.Value);
value = value * pi;
edit_a_hdl.Value = sprintf('%f', value);
and, as you can see, we have our new value:
Upvotes: 1
Reputation: 133
Simulink blocks in many toolboxes are created using MATLAB System objects. If you want to create a new parameter for an existing Simulink block, you may want to create a public property in the shipped System object code. If you are creating your own Simulink block, then writing your code in MATLAB system objects will be very friendly to change/create parameters as you wish.
Simulink extension system object can be created as follows:
To create a Simulink block from a System object, create "MATLAB system" block from the existing Simulink blocks and call your system object from the MATLAB system.
All public properties in the system object code are visible in Simulink mask dialog as shown in below picture.
Hope this is what you are looking for.
Upvotes: 1