Effacer les filtres
Effacer les filtres

How to write cell array to csv file

6 vues (au cours des 30 derniers jours)
Lennaert
Lennaert le 16 Oct 2012
I'm running into difficulties writing my cell array into a csv file. Below is a short description of what I have done to obtain the cell array. Using text scan on 5 csv files with same format but different data I obtained a <1x26 cell> with columns containing both cells and doubles. Now after doing some work with the files I'm left with a cell array in the same format:
Columns 1 through 4
{9268x1 cell} [9268x1 double] [9268x1 double] [9268x1 double]
Columns 5 through 8
[9268x1 double] [9268x1 double] [9268x1 double] [9268x1 double]
Columns 9 through 12
[9268x1 double] [9268x1 double] [9268x1 double] [9268x1 double]
Columns 13 through 16
[9268x1 double] [9268x1 double] [9268x1 double] [9268x1 double]
Columns 17 through 20
[9268x1 double] [9268x1 double] {9268x1 cell} {9268x1 cell}
Columns 21 through 24
[9268x1 double] [9268x1 double] [9268x1 double] [9268x1 double]
Columns 25 through 26
[9268x1 double] {9268x1 cell}
Now my question is: How can i write this back into a csv file? I've tried with fprintf, cell2csv (online) and numerously other online scripts but I haven't found anything to help me do this. Thanks in advance for your help and time.
Regards,
Lennaert
  2 commentaires
Walter Roberson
Walter Roberson le 16 Oct 2012
We need more information on what is inside the {92681x1 cell}.
Lennaert
Lennaert le 17 Oct 2012
Hey Walter, Thank you for your reply. The 92681x1 cell contains string data in the form of dd/mm/yyyy HH:mm:ss, the other 92681x1 cells contain strings such as 'warm', 'cold' or other words similar to those. The [9268x1 double} contain numbers such as 78.2 or 2000 etc.
Thanks in advance!

Connectez-vous pour commenter.

Réponse acceptée

Pedro Villena
Pedro Villena le 17 Oct 2012
Modifié(e) : Pedro Villena le 5 Nov 2012
data = {{'asd';'qew';'zxc'} [1;2] [4;5;6] [7;8;9] [10;11;12;13]};
fid = fopen('csvfilename.csv','w');
for iii=1:length(data)-1,
maxNRows = max([length(data{iii}) length(data{iii+1})]);
end
for ii=1:maxNRows, %%1 -> 47165 rows
for i=1:length(data), %%1 -> 26 columns
try
if iscell(data{i}),
fprintf(fid,'%s,',cell2mat(data{i}(ii)));
else
fprintf(fid,'%f,',data{i}(ii));
end
catch ME
fprintf(fid,',');
end
end
fprintf(fid,'\n');
end
fclose(fid);
  1 commentaire
Lennaert
Lennaert le 17 Oct 2012
Dear Pedro Villena, Thank you so much for your response. Your function seems to work for one of arrays. However with the following cell: Columns 1 through 4
{47165x1 cell} [47165x1 double] [47165x1 double] [47165x1 double]
Columns 5 through 8
[47165x1 double] [47165x1 double] [47165x1 double] [47165x1 double]
Columns 9 through 12
[47165x1 double] [47165x1 double] [47165x1 double] [47165x1 double]
Columns 13 through 16
[47165x1 double] [47165x1 double] [47164x1 double] [47164x1 double]
Columns 17 through 20
[47164x1 double] [47164x1 double] {47164x1 cell} {47164x1 cell}
Columns 21 through 24
[47164x1 double] [47164x1 double] [47164x1 double] [47164x1 double]
Columns 25 through 26
[47164x1 double] {47164x1 cell}
I get an error: Index exceeds matrix dimensions. This is probably do to the fact that colmns 1 - 14 are of dimensions [47165x1] and the rest have [47164x1]. Is there a way to overcome this error without making the cells containing one row too little longer?
Kind regards, Lennaert de Boer

Connectez-vous pour commenter.

Plus de réponses (1)

Peter Perkins
Peter Perkins le 16 Oct 2012
Lennaert, if you have access to the Statistics Toolbox, you may find using dataset arrays is one way to go. Hard to say without knowing what's actually in your CSV files, but if it's a mixture of strings and numbers, you should be able to read the files into dataset arrays using the dataset constructor, combine them (not sure what you need to do there), and then use the export method to write back out to a CSV file.
  1 commentaire
Lennaert
Lennaert le 17 Oct 2012
Hey Peter,
I tried this before, but the problem I was running into was that in some of my csv files the last line was incomplete or in the form of: %s,,%n,%n,,%s. The dataset import function was unable to read the last line and thus couldn't return this. Using textscan i didnt have this issue.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by