加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Matlab从文件中读取中文为乱码的解决方法

(2008-04-21 17:05:54)
标签:

matlab

中文

乱码

分类: Matlab编程
现象:
    MATLAB中用fgetl读出来的中文显示为乱码。
解决方法:
    读出来的内容需要使用native2unicode函数转换一下。
 
测试文件一:
----------------
% test_fgetl1.m 测试fgetl函数
    中文为什么读出来是乱码
fid = fopen('test_fgetl1.m');
while 1
    tline = fgetl(fid);
    if ~ischar(tline),   break,   end
    disp(tline)
end
fclose(fid);
-----------------
现象:
中文部分显示为乱码。
 
测试文件二:
----------------
% test_fgetl2.m 测试fgetl函数
    中文为什么读出来是乱码
fid = fopen('test_fgetl2.m');
while 1
    tline = native2unicode(fgetl(fid));
    if ~ischar(tline),   break,   end
    disp(tline)
end
fclose(fid);
-----------------
现象:
中文部分显示正常,但出现无限循环。
原因:
估计native2unicode函数将文件结束符也转换成字符了。
 
测试文件三:
----------------
% test_fgetl3.m 测试fgetl函数
    中文为什么读出来是乱码
fid = fopen('test_fgetl3.m');
while 1
    tline = fgetl(fid);
    if ~ischar(tline),   break,   end
    tline =  native2unicode(tline);
    disp(tline)
end
fclose(fid);
-----------------
现象:
中文显示正常,文件可以正常读完。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有