user8258323
user8258323

Reputation:

How can I create my own parameters or attributes for a block in simulink?

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

Answers (2)

Matteo Ragni
Matteo Ragni

Reputation: 2956

Programmatically create masks

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:

enter image description here

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:

enter image description 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:

enter image description here

There are more information on this topic here.

Setup parameters programmatically for masked Block

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:

enter image description here

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:

enter image description here

Upvotes: 1

Markandeya Janaswamy
Markandeya Janaswamy

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:

Image to show how to create Simulink extension system object

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.

Image to show how to use the MATLAB system object in Simulink

All public properties in the system object code are visible in Simulink mask dialog as shown in below picture.

Image to show how a public property in system object is reflected as a block dialog parameter in Simulink. Hope this is what you are looking for.

Upvotes: 1

Related Questions