0

I created a file that generates a certain number of data points, below is a simplified version. When I open up the text file, the text file rounds the numbers to lower precision. How do I extend the number of significant digits in the text file?

%% Starting and ending location for signal
Ti = 0; %-1e-5
Step = .00000000004; %4e-11
Tend = .00005; %1e-5
%%  Generates data

Total_data = (Tend-Ti)/Step;
V = zeros(1.25e6,2);
Velocity_Profile = zeros((1.25e6)+1,1);
Time_Profile = zeros((1.25e6)+1,1);
k = 1;
for i=Ti:Step:Tend
    V(:,1) = i;
    Velocity = 0;
    if i<0
        Velocity = 0;
    end
    if i>0
        Velocity = 500;
    end
    Velocity_Profile(k,1) = Velocity_Profile(k,1) + Velocity;
    Time_Profile(k,1) = Time_Profile(k,1) + i;
    k = k+1;
end

Velocity_Profile;
Time_Profile;
Profile = [Time_Profile, Velocity_Profile];

fid=fopen(['Test.txt'],'w+');
fprintf(fid,'%f,%f\n',[Time_Profile,Velocity_Profile].');
fclose(fid);
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Alc
  • 1
  • 1

1 Answers1

1

Check the matlab documentation on the fprintf function.

to increase the precision, you can type

fprintf(fid,'%24.18f %24.18f\n',[Time_Profile,Velocity_Profile].');

To print, say, the number with 18 digits of precision. You can also use the format '%18.14e' to print 14 digits of precision, but with the exponential format (say, 1.24e-2). Just carefully read the documentation and you should be able to find whatever fits you best.

Thales
  • 1,181
  • 9
  • 10
  • 1
    Just note that double precision floating point numbers have only about 15 decimal digits of precision. – Cris Luengo Nov 16 '19 at 20:24
  • @CrisLuengo Depending on your situation, you might want to print 17 digits (https://stackoverflow.com/questions/35624419/matlab-vs-c-double-precision/35626253#35626253) – Daniel Nov 17 '19 at 20:01