Thread Subject: writing to a .txt file question?

Subject: writing to a .txt file question?

From: Hydroman S

Date: 9 Jun, 2008 22:13:01

Message: 1 of 3

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?

Subject: writing to a .txt file question?

From: Miroslav Balda

Date: 10 Jun, 2008 04:21:17

Message: 2 of 3

"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

Subject: writing to a .txt file question?

From: Hydroman S

Date: 10 Jun, 2008 06:00:25

Message: 3 of 3

"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?

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com