help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Write a cell array to a textfile


From: Ben Abbott
Subject: Re: Write a cell array to a textfile
Date: Sat, 22 Jan 2011 09:11:37 -0500

On Jan 21, 2011, at 11:34 PM, Alasdair McAndrew wrote:

> On Sat, Jan 22, 2011 at 10:31 AM, Ben Abbott <address@hidden> wrote:
> 
> On Jan 21, 2011, at 5:52 PM, Alasdair McAndrew wrote:
> 
> > This is a bit of a dumb question, I think, but in spite of using Matlab 
> > (and Octave) for years, I actually have very little experience in low level 
> > file handling.  Anyway, I have a file containing text and numerical data 
> > which I can read into a cell array using textread.  But there doesn't seem 
> > to be a comparable "textwrite" to write the amended cell array back to 
> > another text file.
> >
> > So - what is the most foolproof way of writing a cell array, containing 
> > text and numerical data, to a text file?
> >
> > For example, suppose that every row in the array contains a first and last 
> > name, along with age, height (in metres) and weight (in kilograms), like
> >
> > Leaonhard, Euler, 74, 1.6, 72
> >
> > So each row in the array contains five elements: two string values (the 
> > names), one integer value, and two floating point values.
> >
> > Thanks,
> > Alasdair
> 
> Is the example below sufficient?
> 
>        octave:1> a = {"Leaonhard", "Euler", 74, 1.6, 72; "Ben", "Abbott", 48, 
> 1.83, 54};
>        octave:2> fprintf ("%s, %s, %d, %.2f, %.1f\n", a'{:})
>        Leaonhard, Euler, 74, 1.60, 72.0
>        Ben, Abbott, 48, 1.83, 54.0
> 
> Ben
> 
> Thanks very much.  And if I append a file identification variable to fprintf 
> I can write the results to a file.
> 
> cheers,
> Alasdair

Yes. That is correct.

        fid = fopen ("your_file.txt", "w");
        fprintf (fid "%s, %s, %d, %.2f, %.1f\n", a'{:})
        fclose (fid);

Ben


reply via email to

[Prev in Thread] Current Thread [Next in Thread]