MATLAB: How to check the ‘Signal name must resolve to Simulink signal object’ signal property from command-line

automaticallyobjectprogrammaticprogrammaticallyresolvesignalsimulinkworkspace

We are currently generating code from a large number of models and require that all input and output signals be declared as 'extern'. In order to do this, we create Signal.Signal objects with storage class as 'ImportedExtern' for each input/output signal and resolve the signal to the object in the workspace.
Assuming that we have named the signals and created corresponding Simulink.Signal objects in the workspace, we would like to check the 'Signal name must resolve to Simulink signal object' signal property programmatically, using a MATLAB script.

Best Answer

Manipulating parameters of a signal and other Simulink objects can be performed using "get_param" and "set_param" functions. Although parameters of a signal such as the signal name and "Signal name must resolve to Simulink signal object" are changed in the graphical user interface by changing the parameters of a signal line, under the hood these are properties of the outport where the signal originates. So instead of using set_param to operate on the signal line it should be used to operate on the outports where the signal originates.
The 'Signal name must resolve to Simulink signal object' dialog parameter corresponds to the 'MustResolveToSignalObject' parameter of a block's Inport or Outport handle. However, this parameter is read-only for an Inport handle and therefore can only be set via an Outport handle.
In order to set the "MustResolveToSignalObject" parameter the signal must have a name. If it does not then Simulink will produce an error notifying you that this parameter cannot be changed when the name of a signal is empty. 
The following steps can be used from the MATLAB command prompt or a MATLAB script to achieve this functionality:
1. Find all inport and outport blocks in the top-level system:
outport_blocks = find_system(gcs, 'SearchDepth', 1, 'BlockType', 'Outport');
inport_blocks = find_system(gcs, 'SearchDepth', 1, 'BlockType', 'Inport');
where gcs is the current model.
2. Ensure that the output signal of all Inport blocks has MustResolveToSignalObject= 'on':
for i=1:length(inport_blocks)
ph = get_param(inport_blocks{i}, 'PortHandles');
set_param(ph.Outport, 'MustResolveToSignalObject', 'on') ;
end
3. Since it is not allowed to set the 'MustResolveToSignalObject' parameter of an inport handle, trace the signal to the upstream source block of each Outport block and set its Outport handle to MustResolveToSignalObject='on'. Also, since the source block may have multiple outputs, ensure that the correct signal is set by comparing signal names:
for i=1:length(outport_blocks)
ph = get_param(outport_blocks{i}, 'PortHandles');
signal_name = get_param(ph.Inport,'Name');
block_pc = get_param(outport_blocks{i}, 'PortConnectivity');
src_ph = get_param(block_pc.SrcBlock, 'PortHandles');
src_outports = src_ph.Outport;
for j=1:length(src_outports)
if strcmpi(get_param(src_outports(j), 'Name'), signal_name)
set_param(src_outports(j), 'MustResolveToSignalObject', 'on');
end
end
end