help-octave
[Top][All Lists]
Advanced

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

Re: Using cell2csv


From: PhilipNienhuis
Subject: Re: Using cell2csv
Date: Tue, 11 Jun 2013 01:48:09 -0700 (PDT)

Terry Duell wrote
> Hello All,
> I'm running into a few difficulties figuring how best to use "cell2csv" to  
> capture results.
> I really don't have a sound grasp of cell array indexing, and that isn't  
> helping.
> In my script I first generate a cell array "datavals{1}" to hold text  
> describing the contents of each column of data in the csv file.
> On each run through a loop I then add my calculated values as follows...
> 
> datavals{i+1}={filename, val1, val2, val3...};
> 
> When the calculation loops are done, I am trying to write "datavals" to a  
> file, with the aim of having each entry written to the file as a separate  
> record, using the following...
>   for i=1:n
>       cell2csv("filename",datavals{i});
> end
> 
> but this only leaves me with the values for the last write operation, ie  
> the file is being overwritten.
> It doesn't seem like "cell2csv" has an "append" switch.

You're quite right, cell2csv() doesn't have an append option.

If you want it, post a bug report and in the 
- "Category" select "Octave-Forge package"
- "Item group" select "Feature request"
- in bug title mention "io package"

I don't know when I'll have time for it, but it is on my own wish list as
well. Just like having an analogous option for csv2cell()


> I have tried opening a file...
> 
> fp = fopen("filename","a");
> 
> and using
> 
> cell2csv(fp, datavals{i}); within my write loop, but that also ends up  
> with only the last write.
> 
> Can someone please point me to a solution?

Assuming your data are numerical, what you want looks more like a job for
dlmwrite (which has an append option) or even a simple fprintf (see below). 
(You can also use csvwrite but that is a mere wrapper for dlmwrite.)

First write the column titles, next append the data (as numerical array, not
cell array).

If you want to write mixed data (text and numerical) you'd have to wait for
a patched csv2cell, but easier is to fiddle with fprintf somewhere along the
lines of:

fid = fopen (filename)
fmt1 = "%s,%s,%s\n";
fprintf (fid, , datavals{1, :})
fmt2 = "%f,%f,%f\n";
for i=2,end
<Compute val1, val2, val3>
fprintf (fid, fmt2, val1, val2, val3)
endfor
fclose (fid)

For other numbers of val1...valn you could easily have the format strings
fmt1 and fmt2 be adapted automatically to the number of variables to write,
e.g. 

fmt1 = repmat ("%s,", 1, <nr_of_data>);
fmt1(end) = "\n";

...and similarly for fmt2

Philip



--
View this message in context: 
http://octave.1599824.n4.nabble.com/Using-cell2csv-tp4654022p4654035.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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