How to programmatically name blocks when creating a model

28 次查看(过去 30 天)
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 个评论
Nagarajan G
Nagarajan G 2020-5-20
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

请先登录,再进行评论。

采纳的回答

Fangjun Jiang
Fangjun Jiang 2019-7-11
handle=add_block(...);
set(handle, 'Name','YourBlockName');
Make sure the name is unique though. If not, find a way to make it unique.
  1 个评论
Eetu Mäkiö
Eetu Mäkiö 2019-7-11
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by