問題
%數據存儲在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);
這樣做的好處是可以將想添加的字符串一起輸出到txt文件,dlmwrite函數則稍顯麻煩
Solved