MATLAB: How to perform different operations on different signals in a bus input to a S-Function

businputs-functionsimulink

I want to write a S-Function that accepts a bus input and perform different operations to the different signals present in the bus input.

Best Answer

Even though S-Function can accept bus input, they can not output a bus. In fact, the bus input to a S-Function is 'flattened' out. I.e. the bus input is converted into vector and passed to the S-Function. This means the bus input should contain signals of same data type.
The attached model illustrates a method of using S-Function to perform different operations on different signals present in a bus input. The S-Function is designed to accept 3 inputs. First input is the bus and this bus should consist of only 2 signals of same data type. However, these signals could have different length. The second and third inputs should specify the lengths of first and second signal in the bus respectively. This S-Function performs different operation to each of the signals present in the bus. I.e. the first signal is multiplied by 2 and second signal is multiplied by 3. The result of this operation is a vector that can be accessed at the output port of S-Function.
Please execute the following steps to run this example:
1. Download the files ‘sfun_bus_dynsize_ex.c’ and ‘bus_sfun_ex_mathworks.mdl’ and store it in a folder that is in MATLAB path.
2. Compile the S-Function (sfun_bus_dynsize_ex.c) using the following command:
mex sfun_bus_dynsize_ex.c
3. Run the model.
The C-MEX S-Function(sfun_bus_dynsize_ex.c) has extensive comments that explains the commands used.