How do I avoid scientific notation when using fprintf to print numbers into a text document?

212 views (last 30 days)
I am producing an algorithm to automatically produce code for a CNC machine to machine repaired components.
The coordinates are predetermined and I'm using fprintf to print these coordinates in the format of code for a CNC machine in a text file.
An example of some of the script I have already:
% Opening the file to be editted
fid = fopen('G Code.txt', 'w');
% Programming the CNC mill
fprintf(fid, sprintf('N%d G00 G17 G21 G40 G80 G90 G94\r\n', Nc));
Nc = Nc + 5;
% Starting position of cutter
fprintf(fid, sprintf('N%d X%d Y%d Z%d S%d\r\n', Nc, Xc, Yc, (Zc+TC), rpmR));
Nc = Nc + 5;
% Turning the cutter on
fprintf(fid, sprintf('N%d G01 M03\r\n', Nc));
Nc = Nc + 5;
This gives the following in the text file:
N5 G00 G17 G21 G40 G80 G90 G94
N10 X1.815013e+01 Y3.078016e+01 Z210 S9.549297e+03
N15 G01 M03
The problem I have is that the CNC machines at my university do not read the numbers in the scientific notation. Is there a way to print these numbers as their full numbers as oppose to in the scientific notation?
For example:
N5 G00 G17 G21 G40 G80 G90 G94
N10 X18.15013 Y30.78016 Z210 S9549.297
N15 G01 M03
Thanks in advance!
  2 Comments
Geoff Hayes
Geoff Hayes on 18 Mar 2019
Chris - for your code
fprintf(fid, sprintf('N%d X%d Y%d Z%d S%d\r\n', Nc, Xc, Yc, (Zc+TC), rpmR)
you are using %d for all of your parameters but not all of them are integers. Or am I misnunderstanding something? Shouldn't you be using %f instead as
fprintf(fid, sprintf('N%d X%f Y%f Z%d S%f\r\n', Nc, Xc, Yc, (Zc+TC), rpmR)
You could then specify the number of digits to include after the decimal (if needed).

Sign in to comment.

Accepted Answer

dpb
dpb on 18 Mar 2019
Edited: dpb on 18 Mar 2019
If you want/need a specific format, say so... :)
When you use '%d' for a floating point value, since it expects an integer value you ended up with the default '%e'.
Use
fmt='N%d X%0.5f Y%0.5f Z%0.5f S%0.5f\n';
fprintf(fid, fmt, Nc, Xc, Yc, (Zc+TC), rpmR);
Salt to suit the particular precision needed; I used five decimal digits for all fields; if there's a limit to the number of decimals in a given field the CNC machine can parse, set the corresponding format accordingly.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!