MATLAB Answers

Instead of Hebrew characters I can see only gibberish

11 views (last 30 days)
Lior
Lior on 3 Mar 2015
Commented: Walter Roberson on 15 Sep 2018
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.

Answers (1)

Afiq Azaibi
Afiq Azaibi on 2 Oct 2017
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 Comments
Walter Roberson
Walter Roberson on 15 Sep 2018
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!