1. 程式人生 > 其它 >Matlab如何不以 科學計數法 將資料寫入txt文件

Matlab如何不以 科學計數法 將資料寫入txt文件

問題

%資料儲存在chabu_all變數中,匯出至fiveaxis.txt檔案中
fid = fopen('fiveaxis.txt','at+');
str=['G01 G90 ','X',num2str(chabu_all(1,1)),' Y',num2str(chabu_all(1,2)),' Z',num2str(chabu_all(1,3)),' A',num2str(rad2deg(chabu_all(1,4))),' C',num2str(rad2deg(chabu_all(1,5))),' F',num2str(vc(1)*60)];
fprintf(fid,'%s \n',str);

上述程式碼輸出後,在記事本開啟fiveaxis.txt可能會產生如下效果:
G01 G90 X10 Y2e-4 Z0.01 A10.2 C1e-5 F600
這是因為matlab會在小於0.001時自動使用科學計數法,例如資料為0.0001時,matlab輸出為1e-4

解決辦法

大致思路是給輸出資料設定一個精度限制,matlab就不會以科學計數法輸出,
將num2str函式替換為char(vpa())即可。

%資料儲存在chabu_all變數中,匯出至fiveaxis.txt檔案中
fid = fopen('fiveaxis.txt','at+');
str=['G01 G90 ','X',char(vpa(chabu_all(1,1)),8),' Y',char(vpa(chabu_all(1,2)),8),' Z',char(vpa(chabu_all(1,3)),8),' A',char(vpa(rad2deg(chabu_all(1,4))),8),' C',char(vpa(rad2deg(chabu_all(1,5))),8),' F',char(vpa(vc(1)*60),8)];
fprintf(fid,'%s \n',str);

Solved