generating a string as output of an S-function

3 visualizaciones (últimos 30 días)
matlab_addicted
matlab_addicted el 4 de En. de 2020
Editada: Purvaja el 6 de Mzo. de 2025
Hello,
I'm trying to implement a C-S-function which outputs a string rather than a numerical value, but I couldn't find a way to do it. The length of the string is fixed.
How can i do it? Thanks in advance!

Respuesta aceptada

Purvaja
Purvaja el 5 de Mzo. de 2025
Editada: Purvaja el 6 de Mzo. de 2025
Hi,
To display strings as an S-Function output in Simulink, I came across another MATLAB Answer that describes the answer for it.
Above answer specifies that input/output of string is a limitation of S-Function for C. You can find the documentation for same in below links:
  1. https://www.mathworks.com/help/simulink/ug/simulink-strings.html#mw_588e9a27-6f3e-48f4-ad7a-4c66f6b0526c
  2. https://in.mathworks.com/help/simulink/sfg/integrating-existing-c-functions-into-simulink-models-with-the-legacy-code-tool.html#bq4g1es-10:~:text=work1%5B3%5D%5B5%5D)-,Supported%20Data%20Types,-Data%20Type
Instead of directly trying to output in string format, we can use other data type that are compatible. One workaround which worked for me was to output in array of “uint8_T” data type and then using “ASCII to String” block to get in string format.
You can refer following C-Code:
#define S_FUNCTION_NAME displayStringSFunction
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#define STR_LEN 16
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
ssSetNumInputPorts(S, 0);
ssSetNumOutputPorts(S, 1);
ssSetOutputPortWidth(S, 0, STR_LEN); /* Output is a 1x16 vector */
ssSetOutputPortDataType(S, 0, SS_UINT8); /* Data type: uint8 */
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
uint8_T *y = (uint8_T *)ssGetOutputPortSignal(S,0);
/* Define the fixed string; note: length must be <= STR_LEN */
char s[STR_LEN+1] = "Hello, Simulink!";
int i = 0;
/* Copy characters until the null terminator or STR_LEN is reached */
while (s[i] != '\0' && i < STR_LEN) {
y[i] = (uint8_T)s[i];
i++;
}
/* Pad any remaining positions with 0 */
while (i < STR_LEN) {
y[i++] = 0;
}
}
static void mdlTerminate(SimStruct *S) { }
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif
In Simulink after calling S-function, I got output like below:
For more clarification on the functions used, you can refer to the following resources,
  1. S-Function: https://www.mathworks.com/help/simulink/sfg/what-is-an-s-function.html
  2. ASCII to string: https://www.mathworks.com/help/simulink/slref/asciitostring.html
Or you can access release specific documentation using these commands in your MATLAB command window respectively:
web(fullfile(docroot, 'simulink/sfg/what-is-an-s-function.html'))
web(fullfile(docroot, 'simulink/slref/asciitostring.html'))
Hope this solves your doubt!

Más respuestas (0)

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by