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