2

Possible Duplicate:
text('hebrew string') matlab

shalom
I am trying to work with hebrew strings in matlab. but when I am trying to assign hebrew string to a variable, it doens't assign it. for example:

a='א'

a =

any ideas why?

Community
  • 1
  • 1
user552231
  • 1,095
  • 3
  • 21
  • 40
  • 1
    By the way, the answer in that post, which is to call `text(0.6,0.5,'ירוק','fontname','david','rotation',180,'fontsize',50,'color','r')` produces only boxes on my machine, and not Hebrew, probably because I don't have Hebrew characters set up on mine. – Chris A. Oct 13 '11 at 20:51
  • you might also find this useful: [MATLAB: how to display UTF-8-encoded text read from file?](http://stackoverflow.com/questions/6863147/matlab-how-to-display-utf-8-encoded-text-read-from-file) – Amro Oct 13 '11 at 21:03

2 Answers2

2

Aleph is in UTF-16, which matlab represents with its standard 2 byte char format. It probably doesn't support input of it this way.

You probably have to do

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

And then output it some way readable by UTF-16.

If you're trying to simply put Hebrew into a figure title or something, then you can directly write Latex like this:

 title('\aleph')

If you are trying use Matlab for text processing, I think it will work but you might not be able to view the characters in the Matlab command window.

Update: On my system even writing to a file in the Hebrew encoding is not supported:

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

But maybe your machine supports it if you have Hebrew languages set up.

Chris A.
  • 6,817
  • 2
  • 25
  • 43
2

This is what I would do to read/write to files in this case:

%# 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)

For displaying this string, we need to make MATLAB aware of Unicode characters by calling:

feature('DefaultCharacterSet','UTF-8');

Now on the command prompt you can try:

>> str
str =
אאאאאאאאאא

However, displaying the string with the TEXT function failed (Can someone confirm if this answer actually works as claimed?):

hTxt = text(0.1,0.5, str, 'FontName','David', 'FontSize',30);
set(hTxt, uisetfont(hTxt))

TEXT

I even checked that the correct fonts are available:

>> fontsNames = fontinfo();
>> idx = ~cellfun(@isempty, strfind(lower(fontsNames),'david'));
>> fontsNames(idx)'
ans = 
    'David'
    'David Bold'
    'David Regular'
    'David Transparent'

On the other hand, and as I showed in a previous answer of mine, the solution to showing this text in a GUI is to use Java (MATLAB UICONTROL is based on Java Swing components):

figure('Position',[300 300 500 50]), drawnow
uicontrol('Style','text', 'String',str, ...
    'Units','normalized', 'Position',[0 0 1 1], ...
    'FontName','David', 'FontSize',30);

UICONTROL

(note that by using UICONTROL, even the regular 'Arial' font shows the correct output!)

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454