Matlab/Simulink: How to enable external mode interface programmatically

In Matlab/Simulink, you can enable the external mode interface programmatically by using the following sequence of commands after the Simulink model is loaded:

% Enable interface -> External mode
set_param(getActiveConfigSet(gcs), 'ExtMode', 'on')
% Set external mode transport to TCP/IP
set_param(getActiveConfigSet(gcs), 'ExtModeTransport', Simulink.ExtMode.Transports.getExtModeTransportIndex(getActiveConfigSet(gcs), 'tcpip'))