Search code examples
matlabsimulinkmatlab-compiler

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


Solution

  • 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