Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab, utf-8, Cyrillic

load file.txt

Error using load

Unknown text on line number 1 of ASCII file

only.words.txt

"ÐºÐ°Ñ‚ÐµÐ³Ð¾Ñ€Ð¸Ñ ".

How can I load a text file saved in utf-8 (Cyrillic) into matlab and use the TMG matlab toolbox? I'm aware of a similar answer posted some time ago here. It doesn't solve my problem. TMG still doesn't work.

like image 714
Bondrak Avatar asked Jun 03 '26 11:06

Bondrak


1 Answers

In order to handle UTF strings properly, you have to read them from your text file using a binary approach, as follows:

fid = fopen('mytext.txt','rb');
bytes = fread(fid,'*uint8')';
fclose(fid);

txt = native2unicode(bytes,'UTF-8');

At this point, your string will contain the correct values, but Matlab will still be unable to show it properly. To fix this problem, you either have to use the Java Swing underlying labels with a font that supports unicode characters:

import('java.awt.*');
import('java.swing.*');

lbl = JLabel();
lbl.setFont(Font('Arial Unicode MS',Font.PLAIN,30));
lbl.setText(txt);

or the undocumented function that modifies the default character set used by Matlab (which is, by default, set to ISO-8859-1):

feature('DefaultCharacterSet','UTF-8');
like image 85
Tommaso Belluzzo Avatar answered Jun 06 '26 05:06

Tommaso Belluzzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!