Sza*_*lcs 2 unicode matlab encoding mex matlab-engine
如何通过MATLAB Engine或MEX C接口访问MATLAB字符串的基础unicode数据?
这是一个例子.我们将unicode字符放在UTF-8编码文件test.txt中,然后将其读作
fid=fopen('test.txt','r','l','UTF-8');
s=fscanf(fid, '%s')
Run Code Online (Sandbox Code Playgroud)
在MATLAB中.
现在,如果我第一次feature('DefaultCharacterSet', 'UTF-8'),然后从C engEvalString(ep, "s"),然后作为输出我从文件返回UTF-8的文本.这证明MATLAB在内部将其存储为unicode.但是,如果我这样做mxArrayToString(engGetVariable(ep, "s")),我得到的东西unicode2native(s, 'Latin-1')将在MATLAB中给我:所有非拉丁字符1字符替换为字符代码26.我需要的是以任何unicode格式(UTF-8)访问基础unicode数据作为C字符串,UTF-16等),并保留非Latin-1字符.这可能吗?
我的平台是OS X,MATLAB R2012b.
附录:文档明确声明"[mxArrayToString()]支持多字节编码字符",但它仍然只给出了原始数据的Latin-1近似值.
首先,让我分享一下我在网上找到的一些参考文献:
根据mxChar描述,
MATLAB将字符存储为具有多字节字符集的机器上的2字节Unicode字符
MBCS这个术语对我来说有点含糊不清,我认为它们在这种情况下意味着UTF-16(虽然我不确定代理对,这可能使它成为UCS-2).
更新: MathWorks将措辞改为:
MATLAB对Unicode字符使用16位无符号整数字符编码.
该mxArrayToString页面表明它确实处理多字节编码字符(unlinke mxGetString只处理单字节编码方案).不幸的是,没有关于如何做到这一点的例子.
最后,这是MATLAB新闻组中的一个主题,其中提到了几个与此相关的未记录的函数(您可以通过将libmx.dll库加载到像Windows上的Dependency Walker这样的工具来自己找到它们).
这是我在MEX中做的一个小实验:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char str_ascii[] = {0x41, 0x6D, 0x72, 0x6F, 0x00}; // {'A','m','r','o',0}
char str_utf8[] = {
0x41, // U+0041
0xC3, 0x80, // U+00C0
0xE6, 0xB0, 0xB4, // U+6C34
0x00
};
char str_utf16_le[] = {
0x41, 0x00, // U+0041
0xC0, 0x00, // U+00C0
0x34, 0x6C, // U+6C34
0x00, 0x00
};
plhs[0] = mxCreateString(str_ascii);
plhs[1] = mxCreateString_UTF8(str_utf8); // undocumented!
plhs[2] = mxCreateString_UTF16(str_utf16_le); // undocumented!
}
Run Code Online (Sandbox Code Playgroud)
我在C代码中创建三个字符串,分别用ASCII,UTF-8和UTF-16LE编码.然后我使用mxCreateStringMEX函数(以及其他未记录的版本)将它们传递给MATLAB .
我通过咨询Fileformat.info网站得到了字节序列: A(U + 0041),À(U + 00C0)和水(U + 6C34).
让我们在MATLAB中测试上面的函数:
%# call the MEX function
[str_ascii, str_utf8, str_utf16_le] = my_func()
%# MATLAB exposes the two strings in a decoded form (Unicode code points)
double(str_utf8) %# decimal form: [65, 192, 27700]
assert(isequal(str_utf8, str_utf16_le))
%# convert them to bytes (in HEX)
b1 = unicode2native(str_utf8, 'UTF-8')
b2 = unicode2native(str_utf16_le, 'UTF-16')
cellstr(dec2hex(b1))' %# {'41','C3','80','E6','B0','B4'}
cellstr(dec2hex(b2))' %# {'FF','FE','41','00','C0','00','34','6C'}
%# (note that first two bytes are BOM markers)
%# show string
view_unicode_string(str_utf8)
Run Code Online (Sandbox Code Playgroud)

我正在利用嵌入式Java功能来查看字符串:
function view_unicode_string(str)
%# create Swing JLabel
jlabel = javaObjectEDT('javax.swing.JLabel', str);
font = java.awt.Font('Arial Unicode MS', java.awt.Font.PLAIN, 72);
jlabel.setFont(font);
jlabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
%# place Java component inside a MATLAB figure
hfig = figure('Menubar','none');
[~,jlabelHG] = javacomponent(jlabel, [], hfig);
set(jlabelHG, 'Units','normalized', 'Position',[0 0 1 1])
end
Run Code Online (Sandbox Code Playgroud)
现在让我们反向工作(接受MATLAB中的字符串到C):
#include "mex.h"
void print_hex(const unsigned char* s, size_t len)
{
size_t i;
for(i=0; i<len; ++i) {
mexPrintf("0x%02X ", s[i] & 0xFF);
}
mexPrintf("0x00\n");
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char *str;
if (nrhs<1 || !mxIsChar(prhs[0])) {
mexErrMsgIdAndTxt("mex:error", "Expecting a string");
}
str = mxArrayToString_UTF8(prhs[0]); // get UTF-8 encoded string from Unicode
print_hex(str, strlen(str)); // print bytes
plhs[0] = mxCreateString_UTF8(str); // create Unicode string from UTF-8
mxFree(str);
}
Run Code Online (Sandbox Code Playgroud)
我们从MATLAB内部测试这个:
>> s = char(hex2dec(['0041';'00C0';'6C34'])'); %# "\u0041\u00C0\u6C34"
>> ss = my_func_reverse(s);
0x41 0xC3 0x80 0xE6 0xB0 0xB4 0x00 %# UTF-8 encoding
>> assert(isequal(s,ss))
Run Code Online (Sandbox Code Playgroud)
最后我应该说,如果由于某种原因你仍然遇到问题,最简单的方法是将非ASCII字符串转换为uint8数据类型,然后再将其从MATLAB传递到引擎程序.
所以在MATLAB过程中做:
%# read contents of a UTF-8 file
fid = fopen('test.txt', 'rb', 'native', 'UTF-8');
str = fread(fid, '*char')';
fclose(fid);
str_bytes = unicode2native(str,'UTF-8'); %# convert to bytes
%# or simply read the file contents as bytes to begin with
%fid = fopen('test.txt', 'rb');
%str_bytes = fread(fid, '*uint8')';
%fclose(fid);
Run Code Online (Sandbox Code Playgroud)
并使用Engine API访问变量:
mxArray *arr = engGetVariable(ep, "str_bytes");
uint8_T *bytes = (uint8_T*) mxGetData(arr);
// now you decode this utf-8 string on your end ...
Run Code Online (Sandbox Code Playgroud)
所有测试都是在运行R2012b的WinXP上使用默认字符集完成的:
>> feature('DefaultCharacterSet')
ans =
windows-1252
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助..
在MATLAB R2014a中,许多未记录的 C函数从libmx库中删除(包括上面使用的函数),并替换为在命名空间下公开的等效C++函数matrix::detail::noninlined::mx_array_api.
它应该很容易调整上面的例子(如解释在这里),以最新R2014a版本上运行.