MATLAB Answers

0

How to programmatically name blocks when creating a model

Asked by Eetu Mäkiö on 11 Jul 2019
Latest activity Commented on by Eetu Mäkiö on 11 Jul 2019
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.

  0 Comments

Sign in to comment.

1 Answer

Answer by Fangjun Jiang
on 11 Jul 2019
 Accepted Answer

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

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.