MATLAB: How to get information about the structure of a Simscape model (i.e. which blocks are used and how they are connected)

scriptSimscapesimulink

I need to get information about the used blocks in a Simscape model and how they are connected with each other. I already tried to use the find_system command without any success. Is there a way to get easy access to the blocks and connections of a simscape model?
I appreciate if anyone could help me.

Best Answer

Each block has a parameter called 'PortConnectivity'. The value of this property is an array of structures, each of which describes one of the block's input or output ports. Here is some code that shows how to programmatically find the connected blocks in a Simscape model:
ssc_bridge_rectifier
diode_conns = get_param('ssc_bridge_rectifier/Diode 1',...
'PortConnectivity');
for port_i=1:length(diode_conns)
bh = diode_conns(port_i).DstBlock;
for block_i = 1:length(bh)
disp(['Port ' num2str(port_i) ': ' ...
get_param(bh(block_i),'Name')]);
end
end
You will see this response in the command window:
Port 1: Ideal Transformer
Port 1: Diode 3
Port 2: Diode 2
Port 2: Voltage
Sensor
Port 2: Load
Port 2: C