MATLAB: How to get the port name of a C-MEX S-function in Simulink

c - mexfromgetnameparamaterports-functionsimulinkstring

I would like an example that explains how to get the port name of a C-MEX S-function in Simulink.

Best Answer

Following is an example of how to obtain the port name of a C-MEX S-function in Simulink.
If you open up the callbacks of the S-function block in the attached model, you will notice the following lines in the 'InitFcn':
q=get_param('bsp/my_block','PortHandles');
portname=get_param(q.Inport,'Name');
Here the first line gets the port handles of the S-function block and the second line assigns the input port name of the S-function to the variable “portname”. The string variable “portname” is passed as a parameter into the S-function.
If you open up the attached S-function source file, you will notice the following lines in mdlOutputs:
buflen = mxGetN((ssGetSFcnParam(S, 0)))*sizeof(mxChar)+1;
Port_name = mxMalloc(buflen);
status = mxGetString((ssGetSFcnParam(S, 0)),Port_name,buflen);
mexPrintf("The Input Port Name is - %s\n ", Port_name);
Here the first two lines allocate memory for the string parameter that is being passed into the S-function. Then you would use "mxGetString" to store this string in a Character array. In this example the parameter is being stored in a "char *" called "Port_name" . The parameter "buflen" will be the length of this string.