这就是我在这种情况下读取/写入文件的方法:
%# some Hebrew characters
hebrewString = repmat(char(1488),1,10); %# 'אאאאאאאאאא'
%# convert and write as bytes
b = unicode2native(hebrewString,'UTF-8');
fid = fopen('file.txt','wb');
fwrite(fid, b, '*uint8');
fclose(fid);
%# read bytes and convert back to Unicode string
fid = fopen('file.txt', 'rb');
b = fread(fid, '*uint8')'; %'
fclose(fid);
str = native2unicode(b,'UTF-8');
%# compare and check
isequal(str,hebrewString)
double(str)
为了显示这个字符串,我们需要通过调用让 MATLAB 识别 Unicode 字符:
feature('DefaultCharacterSet','UTF-8');
现在您可以在命令提示符下尝试:
>> str
str =
אאאאאאאאאא
但是,使用TEXT 函数显示字符串失败(有人可以确认这个answer 是否真的像声称的那样工作吗?):
hTxt = text(0.1,0.5, str, 'FontName','David', 'FontSize',30);
set(hTxt, uisetfont(hTxt))
我什至检查了正确的字体可用:
>> fontsNames = fontinfo();
>> idx = ~cellfun(@isempty, strfind(lower(fontsNames),'david'));
>> fontsNames(idx)'
ans =
'David'
'David Bold'
'David Regular'
'David Transparent'
另一方面,正如我在previous answer of mine 中所展示的,在 GUI 中显示此文本的解决方案是使用 Java(MATLAB UICONTROL 基于 Java Swing 组件):
figure('Position',[300 300 500 50]), drawnow
uicontrol('Style','text', 'String',str, ...
'Units','normalized', 'Position',[0 0 1 1], ...
'FontName','David', 'FontSize',30);
(请注意,通过使用 UICONTROL,即使是常规的“Arial”字体也会显示正确的输出!)