How to properly read a csv saved cell array.

8 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2024-8-26
Please upload both:
  • the cell array in a MAT file
  • the CSV file
by clicking the paperclip button.
the cyclist
the cyclist 2024-8-26
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);

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-8-26
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 个)

the cyclist
the cyclist 2024-8-26
编辑:the cyclist 2024-8-26
@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 个评论
Rafael
Rafael 2024-8-26
Thanks a lot! that could be a possible solution but I prefer not to store the cell array directly. Thanks a lot!

请先登录,再进行评论。

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by