help-octave
[Top][All Lists]

## Re: Problem with precision

 From: Fritz Fischer Subject: Re: Problem with precision Date: Thu, 16 Jun 2011 18:55:19 +0200

Hi!

Thanks for you quick responses. Ok, I will rewrite my function.
So far I was using

fprintf(fd,['time', num2str(timeStep),'\n']);

Just to learn something -  can someone tell me what exactly
was causing the problem?

Kind regards,
Walter

2011/6/16 Martijn
Hi Fritz,
Try formatted output:
f = fopen("myfile.txt", "w");
fprintf(f, "%.3f\n", mydata);
fclose(f);

The string "%.3f\n" is the formatting string, saying that every number
is to be written als fixed point with 3 decimal, and is to be followed
by a newline.
To write in two columns with a tab as separation or with a fixed column
width use: "%.3f\t%.3f\n" or "%8.3f%12.3f\n".
Beware that to write a file with 2 columns the data matrix needs to have
two rows. So if:
mydata = [x1, y1; x2, y2; ...; xn, yn]
then use
fprintf(f, "%.3f\t%.3f\n", mydata');
Notice the conjugation! This is a quirk that octave has to be compatible
with Matlab.

good luck,

Martijn

On Thu, 2011-06-16 at 16:34 +0200, Fritz Fischer wrote:
> Hello!
>
> I have a problem with Octave's precision and hope that you can help
> me.
>
> My function writes data to a file, usually using a normal precision.
> But some
> values are written with a very high precision and in the exponential
> format:
>
> 44.875
> 40.262
> 35.327
> 30.005
> 20.981
> 6.9638e-28
> 0
> 0
>
> Can someone tell me how to fix this? I tried format short and format
> long
> but the output is the same at both settings.
>
> Since the files are used as input for another program that does not
> understand the exponential format I need to change this.
>
> Kind regards,
> Walter
>
>
>
>
>
> _______________________________________________
> Help-octave mailing list