From: Walter Roberson on
omegayen wrote:

> I see thanks for clarifying, so if I want MATLAB to generate text files
> I would have to do something such as

> fid = fopen('FileName.txt','wt');
> fprintf(fid,'%16.16f\n',myvariable);
> fclose(fid);

Yes -- though if you do that, you would be printing at least 18 bytes
for each 8 bytes of data. As your file is already more than 2 Gb of
binary, you would need a very good reason to want to use the slow
process of converting it to text for output and then back to binary for
input.

> so if my variable is double, what is the appropriate method for the
> fprintf if I want to keep all the digits but not tack on any extras as
> well... ?

You would need to use a g format instead of f format for that. If I have
figured it correctly, you need at least 17 digits of output in order
to be sure that on input the value will round to the same as the
original value. You won't be outputting the exact decimal equivalent of
the stored value: just enough that no other binary floating point value
would have the same prefix.
From: omegayen on
Walter Roberson <roberson(a)hushmail.com> wrote in message <_7AKn.26623$rU6.20210(a)newsfe10.iad>...
> omegayen wrote:
>
> > I see thanks for clarifying, so if I want MATLAB to generate text files
> > I would have to do something such as
>
> > fid = fopen('FileName.txt','wt');
> > fprintf(fid,'%16.16f\n',myvariable);
> > fclose(fid);
>
> Yes -- though if you do that, you would be printing at least 18 bytes
> for each 8 bytes of data. As your file is already more than 2 Gb of
> binary, you would need a very good reason to want to use the slow
> process of converting it to text for output and then back to binary for
> input.
>
> > so if my variable is double, what is the appropriate method for the
> > fprintf if I want to keep all the digits but not tack on any extras as
> > well... ?
>
> You would need to use a g format instead of f format for that. If I have
> figured it correctly, you need at least 17 digits of output in order
> to be sure that on input the value will round to the same as the
> original value. You won't be outputting the exact decimal equivalent of
> the stored value: just enough that no other binary floating point value
> would have the same prefix.

Thanks for clarifying. Using fwrite() is superior to using fprintf() as far as file size and speed is concerned. So as long as other programs/applications can read in binary it is the preferred method. The only downside is that I can't open the binary file in notepad to inspect it for accuracy.