フィルターのクリア

How do I process a string class in a mex function?

7 ビュー (過去 30 日間)
AJ
AJ 2024 年 4 月 3 日
編集済み: James Tursa 2024 年 4 月 9 日
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 件のコメント
Bruno Luong
Bruno Luong 2024 年 4 月 3 日
編集済み: Bruno Luong 2024 年 4 月 3 日
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.

サインインしてコメントする。

採用された回答

AJ
AJ 2024 年 4 月 9 日
編集済み: AJ 2024 年 4 月 9 日
Given that MATLAB can't/won't support strings in mex environment, I did have to resort to mexCallMATLAB() by calling the char casting operator:
// Convert string to and treat string like a char
rc = mexCallMATLAB(1, &pArrayChar, 1, &(mxArray *)pArrayString, "char");
This is not a great solution, and adds undetectable padding when the string object is an array:
>> testString = ["abd","defegh"];
>> char(testString)
1×6×2 char array
ans(:,:,1) =
'abd '
ans(:,:,2) =
'defegh'
Can string objects be indexed in a mex function, perhaps by using mxGetData()? (If so, how would the pointer be cast?) Or perhaps start by casting it to a cell aray using cellstr (using mexCallMATLAB()).
Thanks for the response.
  1 件のコメント
Bruno Luong
Bruno Luong 2024 年 4 月 9 日
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"]
s = 1x2 string array
"a" "abc"
char(s)
ans = 1x3x2 char array
ans(:,:,1) = 'a ' ans(:,:,2) = 'abc'
cellstr(s)
ans = 1x2 cell array
{'a'} {'abc'}
Cell and char arrays are correctly supported by mex API.

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2024 年 4 月 9 日
編集済み: James Tursa 2024 年 4 月 9 日
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 件のコメント
Bruno Luong
Bruno Luong 2024 年 4 月 9 日
編集済み: James Tursa 2024 年 4 月 9 日
Would using coder to see how string access is converted to C code is a conformation there is no way/API to retrieve data?
AJ
AJ 2024 年 4 月 9 日
編集済み: AJ 2024 年 4 月 9 日
Yes, I did try that approach. Coder (for the mex interface) uses runtime library functions that are not part of the mex function. I could probably adapt that if I took the time to learn how to "marshal in" the strings. But it was more work than I was willing to give. Thanks for the idea.
One more road block here: Coder does not allow arrays of string objects, only scalar strings.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by