【问题标题】:hebrew in the command line - matlab [duplicate]命令行中的希伯来语 - matlab [重复]
【发布时间】:2011-12-07 06:22:04
【问题描述】:

可能重复:
text('hebrew string') matlab

沙龙
我正在尝试在 matlab 中使用希伯来语字符串。但是当我试图将希伯来语字符串分配给一个变量时,它并没有分配它。例如:

a='א'

一个=

任何想法为什么?

【问题讨论】:

  • 顺便说一句,那个帖子中的答案是调用text(0.6,0.5,'ירוק','fontname','david','rotation',180,'fontsize',50,'color','r') 在我的机器上只产生盒子,而不是希伯来语,可能是因为我没有设置希伯来语字符。
  • 您可能还会发现这很有用:MATLAB: how to display UTF-8-encoded text read from file?

标签: string matlab unicode hebrew


【解决方案1】:

这就是我在这种情况下读取/写入文件的方法:

%# 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”字体也会显示正确的输出!)

【讨论】:

    【解决方案2】:

    Aleph is in UTF-16,matlab 以其标准的 2 字节char 格式表示。它可能不支持这种方式的输入。

    你可能不得不这样做

     a = char(1488);  % 1488 is UTF-16 for aleph
    

    然后以某种 UTF-16 可读的方式输出。

    如果你想简单地将希伯来语放入图形标题或其他内容中,那么你可以直接这样写 Latex:

     title('\aleph')
    

    如果您尝试使用 Matlab 进行文本处理,我认为它会起作用,但您可能无法在 Matlab 命令窗口中查看字符。

    更新:在我的系统上,甚至不支持以希伯来语编码写入文件:

     fid = fopen('c:\temp\chris.txt','w','native','hebrew');
     Warning: The encoding 'ISO-8859-8' is not supported.
     See the documentation for FOPEN. 
    

    但如果您设置了希伯来语,也许您的机器支持它。

    【讨论】:

    • 我没有收到任何错误,但它仍然不起作用:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 2018-09-08
    • 2012-12-18
    • 2012-04-03
    相关资源
    最近更新 更多