How to properly read a csv saved cell array.

6 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 26 Aug 2024
Please upload both:
  • the cell array in a MAT file
  • the CSV file
by clicking the paperclip button.
the cyclist
the cyclist on 26 Aug 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);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Aug 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 Comment
Rafael
Rafael on 26 Aug 2024
Thank you so much Walter. That makes totally sense. Thanks again!!

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 26 Aug 2024
Edited: the cyclist on 26 Aug 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 Comment
Rafael
Rafael on 26 Aug 2024
Thanks a lot! that could be a possible solution but I prefer not to store the cell array directly. Thanks a lot!

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!