How to print Chinese characters using disp

23 views (last 30 days)
I have a file that contains u8 Chinese characters. When I open the file with some editor, say Notepad, I can see the Chinese characters no problem. Ok, now I read the file using textread. Then I print a line out of the data I just read and everyting is readable except the Chinese characters. Somehow 8 bit Chinese gets mangled by textread or if it survives the read, it gets mangled by the disp command. How should I read the data? Using a special formatting string? And printing it, using some special output format string??
Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Feb 2020
fread the file *uint8pprecision, instead of fileread. Use native2unicode UTF8 to process the byte sequence into characters. Or is it unicode2native, I keep getting the two confused.
Possibly you will need to GB2312 instead of UTF8
  16 Comments
Walter Roberson
Walter Roberson on 21 Feb 2020
Perhaps you are eligible as Academic for a home institution, to buy at academic price? Formally speaking permission would have to be sought to use the license in a different region, but I don't think that would be a problem.
Fredrik Gustavsson
Fredrik Gustavsson on 28 Mar 2022
Edited: Fredrik Gustavsson on 28 Mar 2022
I'm using R2012b under English Windows 10 and I have the same issue:
>> '你'
ans =
>> double('你')
ans =
26
Our customer would like to have our massive program transated to Chinese. We tried but only the UI controls get translated properly. Our customer tried to display the graphs using Windows in Chinese language but that did not help (since this is a Matlab problem). All text belonging to graphs comes out with nonsense characters. I really wish there would be a workaround. Porting the code to R2016a (or was it 2016b, can't remember exactly) seems like an owerwhelming task.

Sign in to comment.

More Answers (1)

Marc Turcotte
Marc Turcotte on 19 Feb 2020
Edited: Walter Roberson on 19 Feb 2020
MATLAB Version: 7.14.0.739 (R2012a) Windows 10 Home 64 bit
Your comment is very instructive because here is a piece of code that someone on Baidu (Chinese search engine) has posted:
str='你好'
bianma=unicode2native(str);
disp(bianma);
pp=native2unicode(bianma);
disp(pp)
Whereas the output is the same as the input, this does not work on my system. It fails at the assignement:
>> str='你好‘
str='‘
Error: A MATLAB string constant is not terminated properly.
I understand Matlab uses 16 bit unsigned for characters but I always thought that the Chinese characters only needed 8 bits. In any case, there seems to be a default issue.
>> str=uint16('你')
str =
26
does not bomb but it does not mean that it works since I've no idea whether 26 is the right code for 你 。
I don't want to switch my system native language to Chinese but it seems like a likely source of the problem I encountered
  2 Comments
Marc Turcotte
Marc Turcotte on 19 Feb 2020
Edited: Walter Roberson on 28 Mar 2022
By the way,
str=uint16('你好')
bianma=unicode2native(str);
disp(bianma);
pp=native2unicode(bianma);
disp(pp)
produces the following output:
str =
63 63
Undefined function 'unicode2native' for input arguments of type 'uint16'.
Error in readTest_3 (line 5)
bianma=unicode2native(str);
Since the two characters seem to be both assigned 63, this seems wrong since they are not the same; 你好 ni hao means hello. And the unicode2native call then fails for some type mismatch. I think it wants unsigned 8 bit.
There seems to be considerable issues and questions on the web everywhere concerning this change of encoding question and besides being useful to me right now, I have the feeling lots of users might appreciate the resolution, if there can be one... Maybe your Chinese support site would have some useful suggestions?
Best,
m
Walter Roberson
Walter Roberson on 28 Mar 2022
In current releases:
s = '你好';
str = uint16(s)
str = 1×2
20320 22909
bianma = unicode2native(s)
bianma = 1×6
228 189 160 229 165 189
pp = native2unicode(bianma)
pp = '你好'

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!