How can I get the port name of a C-MEX S-function in Simulink?

21 views (last 30 days)
I would like an example that explains how to get the port name of a C-MEX S-function in Simulink.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 11 Jun 2019
Edited: MathWorks Support Team on 11 Jun 2019
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.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!