MATLAB: How to get the exact Simulink.Bus object name inside the S-function that accept Bus input/output

simulink

I have a Bus input to the S-function. The Bus object has a specific name (myBusObj). The S-Function block has a Mask applied to it to allow users to specify the input Bus object as a string. In the Mask Editor, this input field has a type of "DataTypeStr". The provided data type is to be parsed from the S-Function parameter field and to be set as the data type of the input port of the S Function block. Also I want to be able to get the exact Bus object name from inside the S-function. I have tried with 'mxGetClassName', but I only get 'Simulink.Bus' as the returned Bus object name instead of 'myBusObj'. 

Best Answer

When using custom data types (Bus, Enum) in S-Function blocks, these custom types need to be registered first before use. The general approach is to
1. Register the data type from S-function parameters (ssRegisterTypeFromParameter).
2. Set the input/output port data type (ssSetInputPortDataType/ssSetOutputPortDataType).
3. Get the data type name using ssGetDataTypeName.
Attached please find an example which demonstrates how to set the input data type of a S-Function block to be of an Bus type, "MyBusObj". The included files are
- busexample.c (source code for the S-Function block).
- busExample1.slx (Simulink model file containing the S-Function block).
- MyBus.mat (MATLAB data file contains the MyBusObj object).