Effacer les filtres
Effacer les filtres

passing unicode string from C mex function to Matlab

1 vue (au cours des 30 derniers jours)
Christian
Christian le 9 Avr 2013
Hello, I need to send a unicode string from my C mex function to Matlab. At the moment, I try to use mxCreateString to create the variable to return, but unfortunately, I do not get the correct string in Matlab. The string in my C mex function is UTF8 encoded. As far as I can see, the mxCreateString takes a char* only. Does this mean, that mxCreateString can not handle unicode strings? Is there any other way to return unicode strings to Matlab?
Am I correct that Matlab itself uses UTF16 to store strings?
Thank you in advance,
Christian

Réponse acceptée

Friedrich
Friedrich le 9 Avr 2013
Modifié(e) : Friedrich le 9 Avr 2013
Hi,
at least the doc states that the mxChar type is 2 byte:
What happens when you use mxCreateCharArray together with a mxGetData and a memcopy? Or you use a loop to write the data in and cast accordingly?
mxArry *tmp = mxCreateCharArray(...)
mxChar *charData = (mxChar *)mxGetData(tmp);
for(j=0; j < nelem_tmp; j++) {
charData[j] = (mxChar)Your_string[j]
}
  2 commentaires
Christian
Christian le 9 Avr 2013
Friedrich, this works. I am able to pass my unicode characters to Matlab now. Thank you very much.
Jan
Jan le 9 Avr 2013
@Christian: Did you try this with non-ASCII values? The conversion from UTF-8 should actually fail tremendously with this method.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 9 Avr 2013

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by