For Programmers: Free Programming Magazines  


Home > Archive > Matlab > January 2008 > fprintf to write array to text file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fprintf to write array to text file
Tim

2008-01-11, 4:41 am



Hi All,

I am trying to output a bunch of results to a text file that
will then be opened in excel.

I am finding the formating syntax strings to be largely
incomprehensible to me. The odd mystery of matlab is that
complex things are so simple and then some very simple
things seems bizarrely hard...

All I want to do is:

(1) Write some text (these will be column headers)
(2) Add the contents of a multi-column array below this
without the numbers being converted to scientific notation

Previously, I did something similar by writing the text
lines using fprintf, closing the file and then using the
"save" command to append the array data below this.
Unfortunately this converts all numbers to scientific
notation. I need the numbers to be written as simple numbers
(why does matlab make this so hard - is 1.6 really more
precise when written as 1.6000000e+000?).

%Sample Data:
savefile = 'c:\temp.txt';
result1 = 5.3;
result2 = 3.3;
result3 = 1;
mainArray = [1.2 2.3 3.4 ; 11.3 1.4 5.6 ; 1.5 2.3 10.2]

%Sample Code:
myfile = fopen(savefile ,'wt');
fprintf(myfile,'Column1\tColumn2\tColumn
3\n');
fprintf(myfile, '%g\n',mainArray);

fclose(myfile);


--
This code doesn't work because it stacks all the columns on
top of each other. I tried moving the \t around to no avail.

The following code gets tabs after all the numbers but the
columns are still lost:
fprintf(myfile, '%g\t\n',mainArray(:,1),
'%g\t\n',mainArray(:,2));

I assume there is an easy way to stick a multi-column array
into a text file and preserve the number formatting.

Any suggestions will be greatly appreciated.

Thanks

Tim



zhangfujian@gmail.com

2008-01-11, 8:25 am

On 1=D4=C211=C8=D5, =CF=C2=CE=E74=CA=B108=B7=D6, "Tim " <timbr...@nospamhere=
..nope> wrote:
> Hi All,
>
> I am trying to output a bunch of results to a text file that
> will then be opened in excel.
>
> I am finding the formating syntax strings to be largely
> incomprehensible to me. The odd mystery of matlab is that
> complex things are so simple and then some very simple
> things seems bizarrely hard...
>
> All I want to do is:
>
> (1) Write some text (these will be column headers)
> (2) Add the contents of a multi-column array below this
> without the numbers being converted to scientific notation
>
> Previously, I did something similar by writing the text
> lines using fprintf, closing the file and then using the
> "save" command to append the array data below this.
> Unfortunately this converts all numbers to scientific
> notation. I need the numbers to be written as simple numbers
> (why does matlab make this so hard - is 1.6 really more
> precise when written as 1.6000000e+000?).
>
> %Sample Data:
> savefile =3D 'c:\temp.txt';
> result1 =3D 5.3;
> result2 =3D 3.3;
> result3 =3D 1;
> mainArray =3D [1.2 2.3 3.4 ; 11.3 1.4 5.6 ; 1.5 2.3 10.2]
>
> %Sample Code:
> myfile =3D fopen(savefile ,'wt');
> fprintf(myfile,'Column1\tColumn2\tColumn
3\n');
> fprintf(myfile, '%g\n',mainArray);
>
> fclose(myfile);
>
> --
> This code doesn't work because it stacks all the columns on
> top of each other. I tried moving the \t around to no avail.
>
> The following code gets tabs after all the numbers but the
> columns are still lost:
> fprintf(myfile, '%g\t\n',mainArray(:,1),
> '%g\t\n',mainArray(:,2));
>
> I assume there is an easy way to stick a multi-column array
> into a text file and preserve the number formatting.
>
> Any suggestions will be greatly appreciated.
>
> Thanks
>
> Tim


A for loop may be is useful for your problem.
Try something like this. It works.

savefile =3D 'temp.txt';
result1 =3D 5.3;
result2 =3D 3.3;
result3 =3D 1;
mainArray =3D [1.2 2.3 3.4 ; 11.3 1.4 5.6 ; 1.5 2.3 10.2]


%Sample Code:
myfile =3D fopen(savefile ,'wt');
fprintf(myfile,'Column1\tColumn2\tColumn
3\n');
for i=3D1:3
fprintf(myfile, '%g\t%g\t%g\n',mainArray(i,:));
end

fclose(myfile);
Tim

2008-01-11, 7:23 pm

zhangfujian wrote in message
> A for loop may be is useful for your problem.
> Try something like this. It works.
>
> savefile = 'temp.txt';
> result1 = 5.3;
> result2 = 3.3;
> result3 = 1;
> mainArray = [1.2 2.3 3.4 ; 11.3 1.4 5.6 ; 1.5 2.3 10.2]
>
>
> %Sample Code:
> myfile =fopen(savefile ,'wt');
> fprintf(myfile,'Column1\tColumn2\tColumn
3\n');
> for i=1:3
> fprintf(myfile, '%g\t%g\t%g\n',mainArray(i,:));
> end
>
> fclose(myfile);


Hi,

Thanks for your suggestion. I will implement it that way.
And tha

Just to clarify how the formatting strings work (now that
you've reminded me)...
You put the whole formatting string in quotes, where the
specific strings for each column of data are designated by a
"%" as in: '%g\t%g\t%g\n' That formats the three columns of
data and separates them by tabs with a carriage return at
then end. Is that the basic format?

Two more questions...
(1) So there is really no other way to add a whole array to
a text file?
It seems hard to believe that's the best the matlab folks
can offer to get reasonably formatted data into a text file.

(2) You can append an array to an existing file like this:
save(savefile,'mainArray', '-ASCII', '-APPEND')

But as I noted previously, this formats all numbers to
scientific notation even if they are small integers. So is
there really no way to choose what format an array of
numbers is saved in when using the SAVE command?

Thanks again,

Tim
Walter Roberson

2008-01-11, 7:23 pm

In article <fm8b2i$hhn$1@fred.mathworks.com>,
Tim <timbrown@nospamhere.nope> wrote:
>So is
>there really no way to choose what format an array of
>numbers is saved in when using the SAVE command?


doc save

-ascii Save data in 8-digit ASCII format.
-ascii -double Save data in 16-digit ASCII format


That's the limit of your choice. If you want other formats,
use other commands.
--
We regret to announce that sub-millibarn resolution bio-hyperdimensional
plasmatic space polyimaging has been delayed until the release
of Windows Vista SP2.
Walter Roberson

2008-01-11, 7:23 pm

In article <fm8b2i$hhn$1@fred.mathworks.com>,
Tim <timbrown@nospamhere.nope> wrote:
>zhangfujian wrote in message
[color=darkred]
>(1) So there is really no other way to add a whole array to
>a text file?


cvswrite(), dlmwrite()

You can set the cell delimiter to be a tab or space.

dlmwrite offers a 'precision' option which can be the number
of significant digits or a format specifier string.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Dan Hensley

2008-01-11, 7:23 pm

Tim wrote:
>
> Hi All,
>
> I am trying to output a bunch of results to a text file that
> will then be opened in excel.
>
> I am finding the formating syntax strings to be largely
> incomprehensible to me. The odd mystery of matlab is that
> complex things are so simple and then some very simple
> things seems bizarrely hard...



If you knew C, you'd recognize that the fprintf formatting is identical.
So that's where it came from. And it's not terribly difficult to use
once you spend some time learning the basics of the formatting.


> All I want to do is:
>
> (1) Write some text (these will be column headers)
> (2) Add the contents of a multi-column array below this
> without the numbers being converted to scientific notation
>


> %Sample Data:
> savefile = 'c:\temp.txt';
> result1 = 5.3;
> result2 = 3.3;
> result3 = 1;
> mainArray = [1.2 2.3 3.4 ; 11.3 1.4 5.6 ; 1.5 2.3 10.2]
>


> %Sample Code:
> myfile = fopen(savefile ,'wt');
> fprintf(myfile,'Column1\tColumn2\tColumn
3\n');
> fprintf(myfile, '%g\n',mainArray);


You're very close. Try this. Replace the above line with the below line:

fprintf(myfile,'%g\t%g\t%g\n',mainArray.');

Now if you want a generic solution that works with any size matrix, try
this. It expects a 2D matrix:

sa = size(mainArray);
fprintf(myfile,[repmat('%g\t',1,sa(2)-1) '%g\n'],mainArray.');


Dan
dpb

2008-01-11, 7:23 pm

Dan Hensley wrote:
> Tim wrote:

....

....
[color=darkred]
>
> You're very close. Try this. Replace the above line with the below line:
>
> fprintf(myfile,'%g\t%g\t%g\n',mainArray.');

....

To ensure the above request %d would be more certain iianm. The %g will
automagically switch its output depending on the magnitude of the item
iirc, as the G edit descriptor in Fortran (which I'm w/ Tim on; the C
printf() is an abomination and adding the matrix implementation makes
formatted i/o probably in the top two or three of the FAQ list for
Matlab I would think)...(and add a :) to the parenthetical phrase above)...

--
Tim

2008-01-14, 7:29 pm


>
> cvswrite(), dlmwrite()
>
> You can set the cell delimiter to be a tab or space.
>
> dlmwrite offers a 'precision' option which can be the number
> of significant digits or a format specifier string.
> --
> "No one has the right to destroy another person's belief by
> demanding empirical evidence." -- Ann

Landers


That worked great. Thanks! BTW, is it "csvwrite()"


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com