Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: writing to a .txt file question?
Date: Tue, 10 Jun 2008 06:00:25 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 49
Message-ID: <g2l59p$3ld$1@fred.mathworks.com>
References: <g2k9td$83t$1@fred.mathworks.com> <g2kvft$lnc$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1213077625 3757 172.30.248.35 (10 Jun 2008 06:00:25 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 10 Jun 2008 06:00:25 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1376919
Xref: news.mathworks.com comp.soft-sys.matlab:472983



"Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in 
message <g2kvft$lnc$1@fred.mathworks.com>...
> "Hydroman S" <amirgsalem@gmail.com> wrote in message
> <g2k9td$83t$1@fred.mathworks.com>...
> > If I have a matrix 
> > 
> > a=magic(4)
> > 
> >     16     2     3    13
> >      5    11    10     8
> >      9     7     6    12
> >      4    14    15     1
> > 
> > Is it possible to write the matrix data to a *.txt 
file 
> > such that it would look like this:
> > 
> >     60
> >     16     2     3    13
> >      5    11    10     8
> >      9     7     6    12
> >      4    14    15     1
> >      carret return
> > 
> > basically, I need to add the number 60 on top of 
column 1, 
> > and a "carret return" after column 1?  
> > 
> 
> Hi
> Your note on 'column 1' is not very clear, nevertheless, 
if
> the result should look like you presented, the code can 
be
> 
> fid = fopen('test.txt','w');
> fprintf(fid,'%2d\n',60);
> fprintf(fid,'%3d %3d %3d %3d \n', a');
> fprintf(fid,'%s',char(13));
> fclose(fid);
> 
> Mira
> 

Thank you Mira, this works, but what if "a" is not a 
square matrix, i.e a=rand(3,2); I end up with an extra 
value at the end of column 1.  Can you recommed a decent 
ref. on how to use fprintf?