Thread Subject: print a matrix ...

Subject: print a matrix ...

From: Venkat

Date: 22 Oct, 2009 05:18:03

Message: 1 of 7

I am a matlab and programming novice.

Am trying to write a matrix(variable dimensions) to a text file. For instance

A =
1.2 2.0 3.0
2.1 2.3 2.5
3.1 3.4 3.7

should be written with some format to a text file like:

   1.20 2.00 3.00
   2.10 2.30 2.50
   3.10 3.40 3.70

Can someone help. This seems such a silly question, but I need a quick soln to do some post processing. Thanks.

V

Subject: print a matrix ...

From: Branko

Date: 22 Oct, 2009 07:48:03

Message: 2 of 7

"Venkat " <vpisipati@yahoo.com> wrote in message <hbopub$1m3$1@fred.mathworks.com>...
> I am a matlab and programming novice.
>
> Am trying to write a matrix(variable dimensions) to a text file. For instance
>
> A =
> 1.2 2.0 3.0
> 2.1 2.3 2.5
> 3.1 3.4 3.7
>
> should be written with some format to a text file like:
>
> 1.20 2.00 3.00
> 2.10 2.30 2.50
> 3.10 3.40 3.70
>
> Can someone help. This seems such a silly question, but I need a quick soln to do some post processing. Thanks.
>
> V


One possible solution:
infid=fopen('filename','wt');
fprintf(infid,'%.2f %.2f %.2f\n',A');
fclose(infid)

Subject: print a matrix ...

From: Venkat

Date: 22 Oct, 2009 10:26:03

Message: 3 of 7

"Branko " <bogunovic@mbss.org> wrote in message <hbp2nj$bmq$1@fred.mathworks.com>...
> "Venkat " <vpisipati@yahoo.com> wrote in message
>
> One possible solution:
> infid=fopen('filename','wt');
> fprintf(infid,'%.2f %.2f %.2f\n',A');
> fclose(infid)

Unfortunately, the dimensions are not fixed, the matrix f X a dimensions, f and a being variables measured from variable input data. Any help will be deeply appreciated.

V

Subject: print a matrix ...

From: Allan

Date: 22 Oct, 2009 10:43:04

Message: 4 of 7

More or less this:

infid=fopen('filename','wt');
for r=1:size(A,1)
   for c=1:size(A,2)
        fprintf(infid,'%.2f ',A(r,c));
   end
   fprintf(infid,'\n ');
end
fclose(infid)
not quick

Subject: print a matrix ...

From: Steven Lord

Date: 22 Oct, 2009 13:49:43

Message: 5 of 7


"Venkat " <vpisipati@yahoo.com> wrote in message
news:hbpbvr$rrb$1@fred.mathworks.com...
> "Branko " <bogunovic@mbss.org> wrote in message
> <hbp2nj$bmq$1@fred.mathworks.com>...
>> "Venkat " <vpisipati@yahoo.com> wrote in message
>>
>> One possible solution:
>> infid=fopen('filename','wt');
>> fprintf(infid,'%.2f %.2f %.2f\n',A');
>> fclose(infid)
>
> Unfortunately, the dimensions are not fixed, the matrix f X a dimensions,
> f and a being variables measured from variable input data. Any help will
> be deeply appreciated.
>
> V

A = rand(3, 4)
fid = fopen('mydatafile.txt', 'wt');
cols = size(A, 2);
formatString = [repmat('%.4f ', 1, cols) '\n'];
fprintf(fid, formatString, A.');
fclose(fid);
edit mydatafile.txt

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: print a matrix ...

From: us

Date: 22 Oct, 2009 13:55:17

Message: 6 of 7

"Venkat " <vpisipati@yahoo.com> wrote in message <hbopub$1m3$1@fred.mathworks.com>...
> I am a matlab and programming novice.
>
> Am trying to write a matrix(variable dimensions) to a text file. For instance
>
> A =
> 1.2 2.0 3.0
> 2.1 2.3 2.5
> 3.1 3.4 3.7
>
> should be written with some format to a text file like:
>
> 1.20 2.00 3.00
> 2.10 2.30 2.50
> 3.10 3.40 3.70
>
> Can someone help. This seems such a silly question, but I need a quick soln to do some post processing. Thanks.
>
> V

you may find this FEX submission helpful

http://www.mathworks.com/matlabcentral/fileexchange/23840

us

Subject: print a matrix ...

From: Venkat

Date: 22 Oct, 2009 21:24:19

Message: 7 of 7

Thanks Steve, Allan and Branko,

Many thanks for your help. Followed Allan's code and it worked. Also found that the function 'dlmwrite' worked perfectly for my needs:

dlmwrite(outfile, MATRIX, 'delimiter', ' ', 'precision', '%8.2f', '-append', 'newline', 'pc');

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
dlmwrite Venkat 22 Oct, 2009 17:29:20
file exchange us 22 Oct, 2009 09:59:07
reference us 22 Oct, 2009 09:59:07
fex us 22 Oct, 2009 09:59:07
fprintf Branko 22 Oct, 2009 03:49:30
matrix to text Venkat 22 Oct, 2009 01:19:01
rssFeed for this Thread

Contact us at files@mathworks.com