MATLAB: How to obtain the source and destination block names and port numbers connected by a signal in Simulink

simulink

If I click on a certain signal in a Simulink model, how can I programmatically obtain the names of the source and destination blocks connected by the signal? Also, how can the port numbers (source and destination) be printed from the Command Window?

Best Answer

The handle to a selected signal can be obtained using the 'find_system' command:
>> h_line = find_system(gcs,'FindAll','on','FollowLinks','on','type','line','Selected', 'on');
The names of the source and destination blocks with respect to the selected signal can be obtained using the 'get_param' command with the 'SrcBlockHandle' and 'DstBlockHandle' parameters:
>> source_block_handle = get_param(h_line, 'SrcBlockHandle');
>> source_block_name = get_param(source_block_handle, 'Name');
Similarly, the 'SrcPortHandle' and 'DstPortHandle' parameters can be used to obtain the port numbers associated with the selected signal:
>> source_port_handle = get_param(h_line, 'SrcPortHandle');
>> source_port_number = get_param(source_port_handle, 'PortNumber');