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


经常在表示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

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM