Add data type conversion block in simulink model using script

8 views (last 30 days)
I have simulink model where I have a lot of input signals. I need to add data type conversion block to all the input signals. How can i do that using script?

Answers (1)

Fangjun Jiang
Fangjun Jiang on 28 Jan 2021
Do a loop, using these functions
delete_line()
add_block()
add_line()
  2 Comments
rest12
rest12 on 29 Jan 2021
Edited: rest12 on 29 Jan 2021
@Fangjun Jiang I am able to do it without loop. But with for loop I am getting error in the add_line about "Invalid Simulink object name". I am sharing my code.
%Without loop version
delete_line('myModel','Subsystem/1','Add/1');
delete_line('myModel','Subsystem/2','Add/2');
add_block('simulink/Commonly Used Blocks/Data Type Conversion', ['myModel','/Data Type Conversion']);
add_block('simulink/Commonly Used Blocks/Data Type Conversion', ['myModel','/Data Type Conversion1']);
add_line('myModel','Subsystem/1','Data Type Conversion/1','autorouting','on');
add_line('myModel','Subsystem/2','Data Type Conversion1/1','autorouting','on');
add_line('myModel','Data Type Conversion/1','Add/1','autorouting','on');
add_line('myModel','Data Type Conversion1/1','Add/2','autorouting','on');
%Loop version
for i = 1:n;
delete_line('myModel',strcat('Subsystem/',num2str(i),'/'), strcat('Add/',num2str(i)));
add_block('simulink/Commonly Used Blocks/Data Type Conversion', strcat('myModel','/Data Type Conversion',num2str(i)));
add_line('myModel', strcat('Subsystem/',num2str(i),'/'),strcat('Data Type Conversion',num2str(i)),'autorouting','on')
add_line('myModel',strcat('Data Type Conversion',num2str(i),'/1'),strcat('Add',num2str(i)),'autorouting','on');
end
Fangjun Jiang
Fangjun Jiang on 30 Jan 2021
Your code looks good. Also a good practice to run line by line before running a loop.
>> strcat('Subsystem/',num2str(i),'/')
ans =
'Subsystem/1/'
Not the same as 'Subsystem/1'

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!