MATLAB: How to automatically change the Tag names for Goto and From blocks to match the names of the subsystem ports they are connected to in Simulink 7.9 (R2012a)

fromgotonamesportsimulinksubsystem

I have several Subsystems with distinct input and output port names. In addition, I am adding Goto and From blocks directly to these ports.
I was wondering if there is any automated way of naming the tags of these blocks so that they match the port names of my Subsystems. Preferably, I am looking for a MATLAB Script that does this.

Best Answer

Please view the attached MATLAB Function and Simulink model.
The MATLAB function labels all the Goto/From Tag blocks surround a subsystem block. The way this function "setTagNames.m" works is the following:
1. You should first open your model (for instance, the portName.mdl model attached). The function can be found by using the block name as follows:
 
setTagNames('portName/Subsystem');
Alternatively, you can call the function with no arguments and it will use the currently selected Simulink block (with the GCB command). So you can select the Subsystem block in the model by clicking once on it and then run the function like this:
 
setTagNames;
2. The function will look through all the inports and outports of the Subsystem block and figure out which blocks are connected directly upstream or downstream (dpending on the block type). At the same time, we use the FIND_SYSTEM command and parse it using the "/" delimiter to find the actual port name. For example, we need to convert the string "portName/Subsystem/PortName1" to just "PortName1".
3. If the blocks connected to the inports or outports of the Subsystem are Goto/From blocks, the names are changed accordingly. Otherwise, nothing is done to the blocks.
This function works on a single subsystem. However, if you would like to loop through several subsystems you could make changes such as:
- Using FIND_SYSTEM to get a cell array of all Subsystem blocks in the model (or in a particular level of the model).
- Changing the input to setTagNames.m to take in a cell array of multiple subsystem names and then running the function on each subsystem.
This function uses several Simulink command-line interface functions. Please refer to the documentation for these commands for more information.