MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Jan Simon
on 14 Mar 2011

I want to convert a Matlab string to a wchar string, such that I can use e.g. either the C-function _wopen? Or to a LPCWSTR under Windows?

Matlab's CHAR arrays use 2 bytes per element. Inside a C-Mex function I can use mxArrayToString to convert a Matlab CHAR array to a C char * vector. The documentation of mxArrayToString state, that "it supports multibyte character sets". Unfortunately this is not explained with any further details.

Currently under Windows I copy the contents of the mxChar array to a wchar_t vector and append a L'\0'. It seems to work, but I'm not really convinced that this is stable. Under Linux a wchar has a length of 4 bytes and the direct copy is not sufficient.

Are Matlab CHARs UTF16 encoded? How does mxArrayToString support multibyte charcter sets exactly? How do I convert mxChar to wchar correctly and efficiently under Windows and Linux?

*No products are associated with this question.*

## 2 Comments

## Kaustubha Govind

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/3198#comment_6778

Jan: If you don't receive a reply here, Tech Support might be able to give you an answer.

## Jan Simon

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/3198#comment_6788

@Kaustubha: Thanks. There is a 2nd level question, which can be answered here only: How many people use Unicode strings already? Matlab decided for 2 byte CHARs a long time ago, but if nobody is able to use this correctly, this feature is simply a waste of memory. The CSSM thread http://www.mathworks.com/matlabcentral/newsreader/view_thread/301249 was a revealation: There

aresome undocumented functions. But the marvelous idea not to use 1-byte CHARs should be accompanied by a documented and stable interface.