Question

I want to save a matrix to a text file, so I can read it by another program. Right now I use:

save('output.txt', 'A','-ascii');

But this saves my file as

6.7206983e+000  2.5896414e-001
6.5710723e+000  4.9800797e-00
6.3466334e+000  6.9721116e-001
5.9975062e+000  1.3346614e+000
6.0224439e+000  1.8127490e+000
6.3466334e+000  2.0517928e+000
6.3965087e+000  1.9721116e+000

But I would like to have them saved without the "e-notation" and not with all the digits. Is there an easy way to do this?

Edit: Thank you! That works just fine. Sorry, but I think I messed up your edit by using the rollback.

Was it helpful?

Solution

I would use the fprintf function, which will allow you to define for yourself what format to output the data in. For example:

fid = fopen('output.txt', 'wt');
fprintf(fid,'%0.6f %0.6f\n', A.');
fclose(fid);

This will output the matrix A with 6 digits of precision after the decimal point. Note that you also have to use the functions fopen and fclose.

OTHER TIPS

Ditto gnovice's solution, if you need performance & custom formatting.

dlmwrite gives you some control (global, not per-field basis) of formatting. But it suffers from lower performance. I ran a test a few years ago and dlmwrite was something like 5-10x slower than the fopen/fprintf/fclose solution. (edit: I'm referring to large matrices, like a 15x10000 matrix)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top