首页 > 其他 > 详细

matlab规定小数点保留4位且非科学计数法格式存储txt

时间:2015-07-06 10:00:00      阅读:789      评论:0      收藏:0      [点我收藏+]

经常在表示matlab值时,它总会把一些小于1的大于1000的数使用科学计数法表示。这有时让人看了很不爽,每次把数据写到文本文件中也是很恶。

所以每次查来查去,这次解决是这样解决的。

1)、前面设置format g;

2)、使用fprintf设置格式为%g。

二、在小数点后某一位四舍五入,即保留几位小数,也经常用到。 

1.数值型 roundn—任意位位置四舍五入 

>>a=123.4567890; 

>>a=roundn(a,-4) 

a =   123.4568 

其中roundn函数功能如下:     

y = ROUNDN(x) rounds the input data x to the nearest hundredth.   %不指定n,精确到百分位

y = ROUNDN(x,n) rounds the input data x at the specified power    %精确到小数点后指定位数n  

 

format g;

a=roundn(a,-4);
b=roundn(b,-4);

fid = fopen(‘a.txt‘,‘wt‘);
fid2=fopen(‘b.txt‘,‘wt‘);

for i=1:M
    for j=1:N
        fprintf(fid,‘%g‘,a(i,j));
        fprintf(fid,‘%c‘,‘,‘);
          
        
        fprintf(fid2,‘%g‘,b(i,j));
        fprintf(fid2,‘%c‘,‘,‘);
        
    end
        fprintf(fid,‘%c\n‘,‘ ‘); 
        fprintf(fid2,‘%c\n‘,‘ ‘); 
end

  

 

matlab规定小数点保留4位且非科学计数法格式存储txt

原文:http://www.cnblogs.com/aminxu/p/4623540.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!