Clear Filters
Clear Filters

How to create a model in Simulink from Matlab?

2 views (last 30 days)
Hi, I'd like to create a small code that allows me to create a Simulink model from the Matlab command window. It is a quite simple Simulink model, constituted only by Subsystem blocks connected to each other, like this:
In addition, inside of each one of the subsystems there could be one or more subsystems connected sililarly or in parallel, like this:
Unfortunately so far the only thing I am able to do is to open the Simulink model, create a subsystem and specify its position, by doing:
% Create the new Simulink model
sys = 'RBD1';
new_system(sys) % Create the model
open_system(sys) % Open the model
% Specify position and offset of the 1st block; position of the other blocks will be referred to this one's
x1 = 100;
y1 = 100;
x2 = x1 + 100;
y2 = y1 + 40;
offset = 160;
pos = [x1 y1 x2 y2]; % [left top right bottom]
% Start adding blocks; for each one the position has to be specified
add_block('built-in/SubSystem',[sys '/Block1'],'Position',pos);
Now I am struggling a lot to give to every subsystem the necessary ports to make the connections, as well as the different Mux and Demux blocks to make the parallel connections. To add the various connection I know I need the add_line function, but without ports it's not of much use.
Can someone help me or give me some more indications?

Answers (1)

goerk
goerk on 18 Dec 2015
You have to add inports and outports to the subsystems.
add_block('built-in/Inport', [modelName '/' subsystemName '/' portName],'Position',position);
add_block('built-in/Outport', [modelName '/' subsystemName '/' portName2],'Position',position);
The blocknames (eg. 'built-in/Inport') are sometimes a bit difficult to find. But you can use the Library-path as a start point. (e.g. 'Simulink/Signal Attributes/Data Type Conversion') Sometimes there are problems when the block name include a linebreak. Then you can try one or two spaces instead.

Products

Community Treasure Hunt

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

Start Hunting!