Simulink: How to query selected signal / line programatically

This is how you can get the currently selected signal line in a Simulink model programmatically using MATLAB code.

lineHandles = find_system(gcs, 'FindAll', 'on', 'Type', 'line', 'Selected', 'on');

This will give you a handle to the selected line(s) in the current system (gcs).

Here’s how to print their name, for example:

lineHandles = find_system(gcs, 'FindAll', 'on', 'Type', 'line', 'Selected', 'on');

for i = 1:length(lineHandles)
    signalName = get(lineHandles(i), 'Name');
    if isempty(signalName)
        fprintf('Selected Line %d: <No Name>\n', i);
    else
        fprintf('Selected Line %d: %s\n', i, signalName);
    end
end

Example output:

Selected Line 1: intermediary