How to append the contents of array cells to .csv files?

20 vues (au cours des 30 derniers jours)
Robert
Robert le 7 Mar 2019
Commenté : dpb le 8 Mar 2019
Hello, I would like to export the values in the cells of an array into a .csv file, such that the contents of each cell array make up a single "value" in the .csv file. For example
data =
4 x 3 cell array
{'information'} {'labels'} {'odds and ends'}
{'more information'} {'more labels'} {'odds and ends"}
{'info'} {'a label'} {'odds and ends'}
{'data'} {'output'} {'abcd'}
If the file ('data.csv') already has data in it, how do I append the data so that I get the following:
information,labels,odds and ends
more information,more labels,odds and ends
info,a label,odds and ends
data,output,abcd
I have tried using dlmwrite('data.csv',data,'-append')
but this produces
i,n,f,o,r,m,a,t,i,o,n,l,a,b,e,l,s,o,d,d,,a,n,d,,e,n,d,s
m,o,r,e,,i,n,f,o,r,m,a...... and so on
I know I can use something like xlswrite, but the file is being opened and closed many times, with a lot of data (and using xlswrite seems to slow the process down).
Any suggestions? Thanks!
Robert
  1 commentaire
dpb
dpb le 7 Mar 2019
You'll have to write with fprintf the content of the cell array dereferenced to the strings...there's a rudimentary example "Export Cell Array to Text File" link at the bottom of the doc page for fprintf that illustrates the basic idea; you'll have to add the comma delimiter to the format as it's space-delimited and you would be well served to add the double-quotes around the strings since you do seem to have embedded blanks that are problematical on import oftentimes.

Connectez-vous pour commenter.

Réponse acceptée

Robert
Robert le 7 Mar 2019
Modifié(e) : Robert le 7 Mar 2019
Thanks to you both! I was hoping there was simply a syntax error I was using that would allow me to read the cell array (or restructure it) to take advantage of the append function in dlmwrite(). Sounds like there is not a quick way to do this after all?
I ended up going a slightly different route. It's simple to place this data in an .csv file (or Excel file) using the xlswrite() function, but opening and closing it slows down the process. I ended up having the data process in sets, then saving the data in fewer, larger pieces to a .csv file using xlswrite(), and a script to identify the first empty line, in order to append the data. As follows (crude and simple, but it worked). Thanks for the suggestions!
% Identify the file name. If the file is a .csv file, the sheet name will simply be the file name
fileid = 'dataoutput.csv'
sheet = 'dataoutput'
line = xlsread(fileid,sheet,'A1:A10000')
linenumber = size(line)
linenumber = linenumber(1,1)
linenumber = num2str(linenumber)
firstemptyrow = ['A',linenumber]
% Then save the data to the file
xlswrite(fileid,dataset,sheet,firstemptyrow)
  20 commentaires
Robert
Robert le 8 Mar 2019
Ah yes. Once again, showing my ability to ignore the obvious! Cheers.
dpb
dpb le 8 Mar 2019
My output was from the straight command window; the command window doesn't have any of the presentation of data in a spreadsheet-like format. I'm guessing (since I don't use them for precisely such reasons) that you were using the variable editor window or whatever it is that TMW calls it, not just typing at the command line.
That tool is what "tried to help" by showing the root content of the cell but that disguised/hid that the actual array element was a cell containing a cell containing a string, not a cell containing a string.
Sometimes trying to be clever and helpful is more troublesome than it is actual help.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 7 Mar 2019
If you must use dlmwrite(), then use
strjoin(arrayfun(@(IDX) strjoin(data(IDX,:), ','), 1:size(data,1), 'uniform', 0), '\n')
and dlmwrite that with a delimiter of '' (empty). Another way of phrasing this is that you need to pre-format all of the output and write it as a single vector with empty delimiter.
... Which is to say that using dlmwrite() for this is not recommended. Just fopen() with 'a' or 'at' mode and write the data yourself.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by