Instead of Hebrew characters I can see only gibberish

조회 수: 10 (최근 30일)
Lior
Lior 2015년 3월 3일
댓글: Walter Roberson 2018년 9월 15일
Hi,
Iv'e written a Matlab code (using PsychToolbox) that includes printing hebrew characters on the screen. If I run the code on my PC it works fine, I can see the hebrew characters in the right way. But if I run the code on a different PC the characters are printed as gibberish and I cant understand why and how to fix it.
Any suggestions?
Thank in advance, Lior.

답변 (1개)

Afiq Azaibi
Afiq Azaibi 2017년 10월 2일
MATLAB writes its .m files in ASCII encoding, meaning that any non-ASCII encoded characters are replaced with the character '?'. Though MATLAB can display characters in other encodings in the Editor or Command line, it will write them as ASCII if a user saves the m file using the MATLAB editor.
A workaround to this is for the user to write this to an MLX file instead. If you create a file with just the following:
disp('קק')
Saving it to an M file and reopening will have the characters appear as a question marks. Saving this as an MLX will retain the characters upon reopening.
  댓글 수: 3
Dvir Haberman
Dvir Haberman 2018년 9월 12일
I'm having a similar problem which I posted here but after more testing it seems to be more foundmental. I saved a file called SourceFile.m containing some hebrew characters: גגעע
Then I ran this code:
fid=fopen('SourceFile.m','r');
line=fgetl(fid);
fclose(fid);
fid=fopen('DestFile.m','w','n','ISO-8859-8');
fwrite(fid,line);
fclose(fid);
I opened DestFile.m and it contained █ (only empty), but the 'line' varaible contains גגעע and when I go and open SourceFile.m I can see גגעע just fine. Font is David btw.
I chose ISO-8859-8 because I saw it should supports hebrew.
Walter Roberson
Walter Roberson 2018년 9월 15일
When you say you opened DestFile.m are you referring to in the MATLAB editor?
When the locale is not set to one of the ones associated with China, Japan, or Korea, then the default in MATLAB is to assume that source files are IOS-8859-1 or Windows-1252 -- or at least that the source files are single byte characters. Your file would not have been able to represent גגעע without using UTF-8 or UTF-16LE or UTF-16BE . MATLAB would display the UTF-16 as... ummmm, my browser crashes when I try to paste the characters here. Blanks and the x multiplication symbols and cent signs.
When you do the fopen to read the file, you should specify the encoding you expect, such as
fid=fopen('SourceFile.m', 'r', 'n', 'UTF16BE');
Then you should not use fgetl(): instead you should use
line = fread(fid, '*uint8');
To do the writing, you should use
translated_line = unicode2native(line, 'ISO-8859-8');
and fwrite() the translated_line

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by