How could i ouput chinese text strings to a txt file?
Show older comments
Hi, I am trying to export a cell string containing traditional Chinese characters to a txt file. Everything displays properly in the console and i can see the characters properly in the variables viewer and the console.
Default Character coding is windows-1252
>> feature('DefaultCharacterSet')
ans =
windows-1252
When i tried using these commands and export all i get is a blank file
>> str = '長和';
>> csvwrite(test,str)
I suspect might be an encoding issue. How could i export it properly?
Thanks. Bryan
Answers (1)
Geoff Hayes
on 7 May 2015
Bryan - I think that you are correct about needing the appropriate encoding though am not sure if you can do that with csvwrite as it doesn't seem to support an encoding parameter (though maybe you can use if you change your system's language settings).
You can try to use open a file for a particular encoding and then write the string. For example, if I do
str = '長和';
fod = fopen('test.txt','wt','n','GBK');
fprintf(fod,'%s',str);
fclose(fod);
where I have chosen the GBK encoding (you may have something different for your version of MATLAB and OS). Now, if I read the string back as
fid = fopen('test.txt','rt','n','GBK');
newstr = fgetl(fid);
fclose(fid);
I see that
newstr =
長和
is as expected.
However, if I try to open the test.txt file in any text viewer (that I have) I do not see the correct text. Perhaps if I had a text file reader that supports GBK encoding it would work okay.
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!