MATLAB Answers

How to programmatically name blocks when creating a model

12 views (last 30 days)
Hello guys,
I'm trying to create a MATLAB script which would auto-create a Simulink model based on a information from an Excel spreadsheet. So far, the creation of the model itself through script is working fine. This Excel might also change in size in the future, so I have opted to use while-loops in model creation to be able to keep the script working even through changes
Problem is, I'd like to name each block that is created in this while-loop based on the names used in this same Excel sheet. So far the model creation part is working, but I'm having trouble figuring out the correct syntax to use to name the blocks individually.
My code looks like this:
number_of_inputs_from_data = 10;
i=1;
input_position_variable = 50;
while number_of_inputs_from_data >= i,
add_block('simulink/Sources/In1', 'AutoCreatedModel/In1', 'MakeNameUnique', 'on')
set_param (gcb, 'position', [1200 input_position_variable 1220 input_position_variable+20]);
set(gcbh, 'Orientation','left');
i= i+1;
input_position_variable = input_position_variable+50;
end
This script will create the input blocks in a nice vertical line as it should. However, using the 'MakeNameUnique' will name the block just "In1", "In2", "In3" etc.
How to change my script so that the naming of these Input block would come from the Excel Spredsheet? The name is found in "ExcelSheet.BlockName" column in the spreadsheet.

  1 Comment

Nagarajan G
Nagarajan G on 20 May 2020
hlo,
i am getting 'x 'no.of blocks based on my number of input data variable.for example am getting In2 ,In3,In4.....But i am not getting In1 block,also all the blocks are appearing at same location.
and also make me clearly how to define different names for each block.
thanks

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 11 Jul 2019
handle=add_block(...);
set(handle, 'Name','YourBlockName');
Make sure the name is unique though. If not, find a way to make it unique.

  1 Comment

Eetu Mäkiö
Eetu Mäkiö on 11 Jul 2019
Thanks, this worked!
Below is the fixed code in case anyone else stumbles into the same problem at some point :)
number_of_inputs_from_data = 10;
i=1;
input_position_variable = 50;
while number_of_inputs_from_data >= i,
handle = add_block('simulink/Sources/In1', 'autoCreatedModel1/In1', 'MakeNameUnique', 'on')
set_param (gcb, 'position', [1200 input_position_variable 1220 input_position_variable+20]);
set(handle, 'Name', ExcelSheet.BlockName(i));
set(gcbh, 'Orientation','left');
i= i+1;
input_position_variable = input_position_variable+50;
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!