Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Output a matrix to a text file

Subject: Output a matrix to a text file

From: David Doria

Date: 18 Oct, 2007 19:54:58

Message: 1 of 12

using this

a=eye(3);
save('myfile.txt', 'a', '-ASCII');

it writes

   1.0000000e+00 0.0000000e+00 0.0000000e+00
   0.0000000e+00 1.0000000e+00 0.0000000e+00
   0.0000000e+00 0.0000000e+00 1.0000000e+00

is there a way to make it just output

1 0 0 ... etc

thanks

david

Subject: Output a matrix to a text file

From: Randy Poe

Date: 18 Oct, 2007 20:43:30

Message: 2 of 12

On Oct 18, 3:54 pm, "David Doria" <daviddo...@gmail.com> wrote:
> using this
>
> a=eye(3);
> save('myfile.txt', 'a', '-ASCII');
>
> it writes
>
> 1.0000000e+00 0.0000000e+00 0.0000000e+00
> 0.0000000e+00 1.0000000e+00 0.0000000e+00
> 0.0000000e+00 0.0000000e+00 1.0000000e+00
>
> is there a way to make it just output
>
> 1 0 0 ... etc
>

Use FPRINTF for formatted output that gives you a
huge amount of control over format.

If you're familiar with fprintf() in C, this will make a lot
more sense, but otherwise you'll have a bit of a learning
curve. Here are two ways to create the file you want:

First way, if you know that a is 3x3:

fid = fopen('myfile.txt', 'wt'); % Open for writing
for i=1:3
   fprintf(fid, '%d %d %d\n', a(i,:));
end
fclose(fid);

More general, if you don't know either dimension of a:

fid = fopen('myfile.txt', 'wt'); % Open for writing
for i=1:size(a,1)
   fprintf(fid, '%d ', a(i,:));
   fprintf(fid, '\n');
end
fclose(fid);

Feel free to ask follow-up questions on decoding any
of that. It's well worth understanding formatted I/O.

           - Randy

Subject: Output a matrix to a text file

From: David Doria

Date: 20 Oct, 2007 23:41:59

Message: 3 of 12

Thanks Randy!

I am actually familiar with fprintf, but I thought matlab
might have an easier way to "matrixize" the output, instead
of doing it a line at a time. Its still very cool that
matlab can use vectors in fprintf, not used to that in c++!!

Thanks,

David

 Randy Poe <poespam-trap@yahoo.com> wrote in message
<1192740210.124487.170900@i38g2000prf.googlegroups.com>...
> On Oct 18, 3:54 pm, "David Doria" <daviddo...@gmail.com>
wrote:
> > using this
> >
> > a=eye(3);
> > save('myfile.txt', 'a', '-ASCII');
> >
> > it writes
> >
> > 1.0000000e+00 0.0000000e+00 0.0000000e+00
> > 0.0000000e+00 1.0000000e+00 0.0000000e+00
> > 0.0000000e+00 0.0000000e+00 1.0000000e+00
> >
> > is there a way to make it just output
> >
> > 1 0 0 ... etc
> >
>
> Use FPRINTF for formatted output that gives you a
> huge amount of control over format.
>
> If you're familiar with fprintf() in C, this will make a lot
> more sense, but otherwise you'll have a bit of a learning
> curve. Here are two ways to create the file you want:
>
> First way, if you know that a is 3x3:
>
> fid = fopen('myfile.txt', 'wt'); % Open for writing
> for i=1:3
> fprintf(fid, '%d %d %d\n', a(i,:));
> end
> fclose(fid);
>
> More general, if you don't know either dimension of a:
>
> fid = fopen('myfile.txt', 'wt'); % Open for writing
> for i=1:size(a,1)
> fprintf(fid, '%d ', a(i,:));
> fprintf(fid, '\n');
> end
> fclose(fid);
>
> Feel free to ask follow-up questions on decoding any
> of that. It's well worth understanding formatted I/O.
>
> - Randy
>

Subject: Output a matrix to a text file

From: dpb

Date: 21 Oct, 2007 00:03:15

Message: 4 of 12

David Doria wrote:
> Thanks Randy!
>
> I am actually familiar with fprintf, but I thought matlab
> might have an easier way to "matrixize" the output, instead
> of doing it a line at a time. ...

There is, TMW was thinking of you already... :)

 >> sprintf('%d %d %d\n', eye(3))
ans =
1 0 0
0 1 0
0 0 1

Note that you need to know the number of elements/row to place the
linefeed where desired. Also, if the data are actual floating point, as
in C one needs the specific cast to integer or the formatted output
routines use an implicit E format.

Also note that while the example used sprintf instead of fprintf to
display the output in the command window to past, will get the same
output to file.

help fprintf

--

Subject: Output a matrix to a text file

From: us

Date: 21 Oct, 2007 00:13:52

Message: 5 of 12

David Doria:
<SNIP not too familiar with ML... yet...

> I am actually familiar with fprintf, but I thought matlab
> might have an easier way to "matrixize" the output,
instead of doing it a line at a time...

of course ML has an easy way of <matrixize>

     help dlmwrite;

% in particular, look at the <precision> option...

us

Subject: Output a matrix to a text file

From: Steven Lord

Date: 21 Oct, 2007 17:42:03

Message: 6 of 12


"dpb" <none@non.net> wrote in message news:ffe55r$gb4$1@aioe.org...
> David Doria wrote:
>> Thanks Randy!
>>
>> I am actually familiar with fprintf, but I thought matlab
>> might have an easier way to "matrixize" the output, instead
>> of doing it a line at a time. ...
>
> There is, TMW was thinking of you already... :)
>
> >> sprintf('%d %d %d\n', eye(3))
> ans =
> 1 0 0
> 0 1 0
> 0 0 1

Note that this doesn't necessarily do what you expect it to do:

>> A = [1 2 3;4 5 6;7 8 9];
>> sprintf('%d %d %d\n', A)

ans =

1 4 7
2 5 8
3 6 9

As mentioned in HELP SPRINTF:

    3. SPRINTF is "vectorized" for the case when A is nonscalar. The
       format string is recycled through the elements of A (columnwise)
       until all the elements are used up.

So you'd want to use:

>> sprintf('%d %d %d\n', A.')

ans =

1 2 3
4 5 6
7 8 9

It worked as you expected in the case you posted because eye(3) is
symmetric.

> Note that you need to know the number of elements/row to place the
> linefeed where desired.

A = [1 2 3;4 5 6;7 8 9;10 11 12];
sprintf([repmat('%d ', 1, size(A, 2)), '\b\n'], A.')

ans =

1 2 3
4 5 6
7 8 9
10 11 12

The '\b' gets rid of the space at the end of the line.

*snip*

--
Steve Lord
slord@mathworks.com

Subject: Output a matrix to a text file

From: David Doria

Date: 21 Oct, 2007 17:44:58

Message: 7 of 12

yayy perfect!

"us " <us@neurol.unizh.ch> wrote in message
<ffe5k0$lur$1@fred.mathworks.com>...
> David Doria:
> <SNIP not too familiar with ML... yet...
>
> > I am actually familiar with fprintf, but I thought matlab
> > might have an easier way to "matrixize" the output,
> instead of doing it a line at a time...
>
> of course ML has an easy way of <matrixize>
>
> help dlmwrite;
>
> % in particular, look at the <precision> option...
>
> us

Subject: Output a matrix to a text file

From: dpb

Date: 21 Oct, 2007 17:48:54

Message: 8 of 12

Steven Lord wrote:
> "dpb" <none@non.net> wrote in message news:ffe55r$gb4$1@aioe.org...
>> David Doria wrote:
>>> Thanks Randy!
>>>
>>> I am actually familiar with fprintf, but I thought matlab
>>> might have an easier way to "matrixize" the output, instead
>>> of doing it a line at a time. ...
>> There is, TMW was thinking of you already... :)
>>
>>>> sprintf('%d %d %d\n', eye(3))
>> ans =
>> 1 0 0
>> 0 1 0
>> 0 0 1
>
> Note that this doesn't necessarily do what you expect it to do:
>
>>> A = [1 2 3;4 5 6;7 8 9];
>>> sprintf('%d %d %d\n', A)
>
> ans =
>
> 1 4 7
> 2 5 8
> 3 6 9
>
...

Yeah, I was just coming back to correct the oversight of forgetting the
transpose but you beat me to it. If only OP hadn't use eye() as the
example...(see, I can still figure out a way to shift blame... :) )

--

Subject: Output a matrix to a text file

From: dpb

Date: 21 Oct, 2007 17:50:22

Message: 9 of 12

us wrote:
> David Doria:
> <SNIP not too familiar with ML... yet...
>
>> I am actually familiar with fprintf, but I thought matlab
>> might have an easier way to "matrixize" the output,
> instead of doing it a line at a time...
>
> of course ML has an easy way of <matrixize>
>
> help dlmwrite;
>
> % in particular, look at the <precision> option...

That's apparently a later release than I have... :(

--

Subject: Output a matrix to a text file

From: University of Edinburgh

Date: 17 Apr, 2011 20:23:02

Message: 10 of 12

Dear People,

sprintf , I was trying to use while outputing like

file_1 = fopen('output1.txt','w')
sprintf(file_1,'%6.2f %12.8f %6.2f %4.5f %9f %9f %9f\n',A')


it gave me some format error
??? Error using ==> sprintf
Invalid format.


Do you people have suggestions ??

Best
Prashant







dpb <none@non.net> wrote in message <ffe55r$gb4$1@aioe.org>...
> David Doria wrote:
> > Thanks Randy!
> >
> > I am actually familiar with fprintf, but I thought matlab
> > might have an easier way to "matrixize" the output, instead
> > of doing it a line at a time. ...
>
> There is, TMW was thinking of you already... :)
>
> >> sprintf('%d %d %d\n', eye(3))
> ans =
> 1 0 0
> 0 1 0
> 0 0 1
>
> Note that you need to know the number of elements/row to place the
> linefeed where desired. Also, if the data are actual floating point, as
> in C one needs the specific cast to integer or the formatted output
> routines use an implicit E format.
>
> Also note that while the example used sprintf instead of fprintf to
> display the output in the command window to past, will get the same
> output to file.
>
> help fprintf
>
> --

Subject: Output a matrix to a text file

From: dpb

Date: 17 Apr, 2011 20:37:22

Message: 11 of 12

On 4/17/2011 3:23 PM, University of Edinburgh wrote:
> Dear People,
> sprintf , I was trying to use while outputing like
>
> file_1 = fopen('output1.txt','w')
> sprintf(file_1,'%6.2f %12.8f %6.2f %4.5f %9f %9f %9f\n',A')
>
>
> it gave me some format error ??? Error using ==> sprintf
> Invalid format.
...

Use fprintf to write to a file, sprintf to write to a string variable.

The error re: invalid format string is 'cuz sprintf() doesn't use a fid;
it expects the first argument to be the format string and file_1
obviously isn't valid one of those.

Also, since is a text file, you'll be certain that newline will be
interpreted correctly for the platform if you use 'wt' in the fopen()
call...

--

Subject: Output a matrix to a text file

From: Virendra Kumar

Date: 21 Mar, 2013 12:13:06

Message: 12 of 12

use "dlmwrite" refer this: http://www.mathworks.in/help/matlab/ref/dlmwrite.html

"David Doria" <daviddoria@gmail.com> wrote in message <ff8dmi$jsk$1@fred.mathworks.com>...
> using this
>
> a=eye(3);
> save('myfile.txt', 'a', '-ASCII');
>
> it writes
>
> 1.0000000e+00 0.0000000e+00 0.0000000e+00
> 0.0000000e+00 1.0000000e+00 0.0000000e+00
> 0.0000000e+00 0.0000000e+00 1.0000000e+00
>
> is there a way to make it just output
>
> 1 0 0 ... etc
>
> thanks
>
> david

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us