2

I would like to pass an uint8 array to my S-Function as a parameter:

inParam = char(uint8(1:7))

In the S-Function I did the followings

UINT8_T *inParam = (UINT8_T *)mxGetPr(ssGetSFcnParam(S, PARAM_IN_PORT_NR)); //;

but I saw that actually the array elements are stored as 2 bytes.

UINT16_T *inPorts = (UINT16_T *)mxGetPr(ssGetSFcnParam(S, PARAM_IN_PORT_NR)); //
// I can loop through the data
// This is only a snippet
*(inPorts++);

Why is that? Is this happening in all the Matlab versions?

Mokus
  • 10,174
  • 18
  • 80
  • 122

1 Answers1

6

MATLAB uses UTF-16 encoding for character vectors and strings. For example, see the definition of matlab::engine::String as a std::basic_string<char16_t>. This is the same on all platforms. I don't know when exactly Unicode support was introduced in MATLAB, but you can assume that any version from the last 15 years uses 16-bit character encoding.

Consider using mxGetString to get an 8-bit (ASCII) representation of the string, or mxArrayToString if you need to support Unicode characters.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • I'm transferring some array with uint8 s data type. Unfortunately MATLAB enables to transfer strings or doubles as S-Function parameters without TLC. Using the above method I can directly get back the numbers. What pitfalls can I end up by doing this? – Mokus Nov 27 '19 at 10:42
  • @OHLÁLÁ Where is the data generated and where is it used? If it is generated and used on the C side only then you can certainly do this. If you are generating or using it on the MATLAB side I would just stick with doubles. The conversion between `uint8` and `char` is likely more costly than just using doubles everywhere. – Cris Luengo Nov 27 '19 at 14:26
  • I am converting the uint8 to char once at the initialization in Matlab, pass it to the S-Function as a parameter and use it there. – Mokus Nov 28 '19 at 09:52