How do I process a string class in a mex function?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I would like to pass a string (not a character array, but a class object of type "string") into a mex function, such as:
myMexFunction_mex("xyzyy")
and then process it. However, there seems to be no mex support for string objects.
The following C code snippet returns true for a string object:
mxIsClass(prhs[0],"string") % returns true
The following returns a value of 19, which is not defined in mex.h as part of the mxClassID enumeration (the highest defined value is 18, mxOBJECT_CLASS):
mxGetClassID(prhs[0]) % returns 19
It seems crazy that the mex API support doesn't support strings. Strings have been around since R2016b or so.
Are there any undocumented features I could use to get access to the string length and data? And what character encoding scheme is used (UTF-8, or UTF-16, say)?
BTW, using a character arrays instead of a string object is not an option for me.
1 comentario
Bruno Luong
el 3 de Abr. de 2024
Editada: Bruno Luong
el 3 de Abr. de 2024
IMO string as all object oriented class has minimal (close to 0) support in Mex API, and TMW does not intend to do anything to impprove this.
Respuesta aceptada
AJ
el 9 de Abr. de 2024
Editada: AJ
el 9 de Abr. de 2024
1 comentario
Bruno Luong
el 9 de Abr. de 2024
Yes you can convert to cell of char array using cellstr(s) rather than char(s). It will not pad trailing blank.
s=["a" "abc"]
char(s)
cellstr(s)
Cell and char arrays are correctly supported by mex API.
Más respuestas (1)
James Tursa
el 9 de Abr. de 2024
Editada: James Tursa
el 9 de Abr. de 2024
Strings are opaque objects (like classdef) and there are no API functions to get pointer access to the data areas. I am unaware of any hacks to get at this data either. Note that this is not just for strings, but is true for other opaque objects in MATLAB such as half, etc.
You are probably stuck with a deep copy into a char array either at the m-code level or inside the mex routine via mexCallMATLAB() ... yes I know you wrote this is not an option :(
2 comentarios
Bruno Luong
el 9 de Abr. de 2024
Editada: James Tursa
el 9 de Abr. de 2024
Would using coder to see how string access is converted to C code is a conformation there is no way/API to retrieve data?
Ver también
Categorías
Más información sobre MATLAB Compiler en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!