User`s guide

Overview of the C MEX S-Function Routines
3-21
Accessing Input Signals of Individual Ports
This section describes how to access all input signals of a particular port and
write them to the output port. The figure above shows that the input array of
pointersmay point to noncontiguousentries in theb lock I/O vector. Theoutput
signalsof aparticularport form acontiguous vector.Therefore, thecorrect way
to access input elements and write them to the output elements (assuming the
input and output ports have equal widths) is to use this code:
int_T element;
int_T portWidth = ssGetInputPortWidth(S,inputPortIndex);
InputRealPtrs uPtrs = ssGetInputPortRealSignalPtrs(S,inputPortIndex);
real_T *y = ssGetOutputPortSignal(S,outputPortIdx);
for (elemet=0; element<portWidth; element++) {
y[element] = *uPtrs[element];
}
A commonmistake is to try and accessthe input signals via pointerarithmetic.
For example, if you were to place
real_T *u = *uPtrs; /* Incorrect */
just below the initialization of uPtrs and replace the inner part of the above
loop with
*y++ = *u++; /* Incorrect */
the code compiles, but the MEX-file may crash Simulink. This is because it is
possible to access invalid memory (which depends on how you build your
model). Whenaccessing the inputsignals incorrectl y, acrashwillhappenwhen
the signals entering your S-function block are not contiguous. Noncontiguous
signal data occur when signals pass through virtual connection blocks such as
the Mux or Selector blocks.
To verify that you are correctly accessing wide input signals, pass a replicated
signal to each input port of your S-function. This is done b y creating a Mux
block with the number of input ports equal to the width of the desired signal