How to properly read a csv saved cell array.

8 vues (au cours des 30 derniers jours)
Rafael
Rafael le 26 Août 2024
Commenté : Rafael le 26 Août 2024
Hi,
I am new with Matlab and I would like to know why I am not reading a cell array as I saved it. i.e. I have a cell of arrays (3834x1) and I saved it as csv file.
writecell(X_train_past, ...
strcat(train_data_path, 'past_features.csv'), ...
"Delimiter",";")
However, when reading the cell I got a cell of 926x864, totally different of the saved cell array. Is there any way to get the same shape when reading a cell as it was writen ?
opts = detectImportOptions(test_path);
opts.LineEnding = '\n';
C = readcell(test_path,opts);
Thanks a lot in advance,
Rafa
  2 commentaires
Stephen23
Stephen23 le 26 Août 2024
Please upload both:
  • the cell array in a MAT file
  • the CSV file
by clicking the paperclip button.
the cyclist
the cyclist le 26 Août 2024
I believe that this code snippet will illustrate fundamental issue, at a more reasonable scale:
% Create a cell array where each element is a numeric array
rng default
x = cell(3,1);
x{1} = rand(2,5);
x{2} = rand(3,5);
x{3} = rand(3,5);
% Write to file
filename = 'past_features.csv';
writecell(x,filename,"Delimiter",";")
% Read back from file
opts = detectImportOptions(filename);
opts.LineEnding = '\n';
C = readcell(filename,opts);

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 26 Août 2024
csv files do not have the capacity to mark cell boundaries. You will never be able to do what you want with a csv file.
xls and xlsx files do not directly have the capacity to mark cell boundaries, but they offer the possibility of writing each cell as a different Sheet. There is no direct support for exporting in this format; you would have to loop using writematrix with sheet specification, and possibly with append mode turned on.
  1 commentaire
Rafael
Rafael le 26 Août 2024
Thank you so much Walter. That makes totally sense. Thanks again!!

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 26 Août 2024
Modifié(e) : the cyclist le 26 Août 2024
@Walter Roberson's answer is the canonical one, to be sure. A csv simply cannot store the cell array as you hoped (and naively coded) it would.
That being said, you may be able to very very kludgily reconstruct (approximately) what got stored in the CSV. They will not be exact (presumably due to some storage differences in floating point), but more importantly is not likely to generalize beyond your specific example (and my miniature version of it). Caveat emptor!
% Create a cell array where each element is a numeric array
rng default
x = cell(3,1);
x{1} = rand(2,5);
x{2} = rand(3,5);
x{3} = rand(3,5);
% Write to file
filename = 'past_features.csv';
writecell(x,filename,"Delimiter",";")
% Read back from file
opts = detectImportOptions(filename);
opts.LineEnding = '\n';
C = readcell(filename,opts);
% Reconstruct it. Relies on the fact that during reconstruction, you know
% the size the cell contents were going in.
[r,c] = size(x);
x_recon = cell(r,c);
for nr = 1:r
tmp = [C{nr,:}];
tmp(isnan(tmp))=[];
x_recon{nr} = reshape(tmp,size(x{nr}));
end
% Compare one row, to illustrate
x{1}
ans = 2x5
0.8147 0.1270 0.6324 0.2785 0.9575 0.9058 0.9134 0.0975 0.5469 0.9649
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x_recon{1}
ans = 2x5
0.8147 0.1270 0.6324 0.2785 0.9575 0.9058 0.9134 0.0975 0.5469 0.9649
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 commentaire
Rafael
Rafael le 26 Août 2024
Thanks a lot! that could be a possible solution but I prefer not to store the cell array directly. Thanks a lot!

Connectez-vous pour commenter.

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by