Adding a subsystem using an m file

I am using the following code to create a simulink model via text:
%%Test model for learning how to build simulink models in script
%Clean up
clear;
clc;
%%open the model
sys = 'testModel';
delete('testModel.mdl')
new_system(sys) % Create the model
open_system(sys) % Open the model
%%Place blocks
pos1 = [0 0 30 30];
pos2 = [80 0 120 30];
offset = [0 60 0 60];
for i = 1:10;
add_block('built-in/Constant',[sys strcat('/Constant',num2str(i))],'Value','7','Position',pos1);
add_block('built-in/gain',[sys strcat('/gain',num2str(i))], 'Gain','3','Position',pos2);
%Connect blocks
add_line(sys,strcat('Constant',num2str(i),'/1'),strcat('gain',num2str(i),'/1'),'autorouting','on')
pos1=pos1+offset;
pos2=pos2+offset;
end
add_block('built-in/Subsystem','/Subsystem1')
% blocks = find_system(sys, 'SearchDepth', 1);
% bh = cell(length(blocks),1);
% for i = 2:length(blocks)
% bh = [bh get_param(blocks{i}, 'handle')];
% end
% Simulink.BlockDiagram.createSubSystem(bh);
%%Save and close system
pause %hit enter to continue
save_system(sys)
close_system(sys)
I want to place all of the blocks I just created into a subsystem, is it possible to do this with add_block command?

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 1 Aug. 2011

1 Stimme

You can do that. The easiest way to do is to add your Subsystem block first.
add_block('built-in/Subsystem','/Subsystem1')
And then add all the other blocks you want by specifying the destination.
add_block('built-in/Constant','/Subsystem1/MyConstant')

3 Kommentare

Michael Joslin
Michael Joslin am 1 Aug. 2011
If I put add_block('built-in/Subsystem','/Subsystem1') just before my loop where all of my blocks are made I get the following error:
??? Error using ==> modelfromscript at 14
A new block named '/Subsystem1' cannot be added.
Arnaud Miege
Arnaud Miege am 1 Aug. 2011
Try:
add_block('built-in/Subsystem','Subsystem1')
Arnaud
Michael Joslin
Michael Joslin am 1 Aug. 2011
I got it.
add_block('built-in/Subsystem',[sys '/Subsystem1'])
I forgot the sys in the brackets.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Paulo Silva
Paulo Silva am 1 Aug. 2011

0 Stimmen

Simulink.BlockDiagram.createSubSystem(blocks)

1 Kommentar

Michael Joslin
Michael Joslin am 1 Aug. 2011
I already have a section of code in my question that performs this. My question was how to add a subsystem and populate it using add_block.

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by