Processing cell arrays as a batch instead of one by one
1 次查看(过去 30 天)
显示 更早的评论
i have 3 x 50 cells of cell array. i want to read those 50 cells row by row at a stretch and writing into a txt file. After executing the code below, the expeted result did not arrive. I inspected cluster_data variable, and i found that its size is 1x4 double instead of 50x4.It can be seen that Instead of reading all the 50 cells in a row, it just read only one cell. What to do?
iniclu = cell(1,1); %final size of iniclu is 3x50 with each cell containing 4 data
fid = fopen('iris_data2.txt', 'a');
for i = 1:3
cluster_data = iniclu{i,:}; % I assuming 50 cells of 1st row is read into cluster_data when i = 1 and so on
fprintf(fid, '%f,%f,%f,%f\n', cluster_data);
end
fclose(fid);
end
Pls clarify!
0 个评论
采纳的回答
Voss
2024-2-26
Perhaps this:
fprintf(fid, '%f,%f,%f,%f\n', iniclu{i,:});
Running example:
% example data (not sure whether this is similar to your data or not):
iniclu = {[1 2 3 4],[5 6 7 8];[9 10 11 12],[13 14 15 16]}
fid = fopen('iris_data2.txt', 'a'); % (I guess append mode is what you want)
for i = 1:size(iniclu,1)
fprintf(fid, '%f,%f,%f,%f\n', iniclu{i,:});
end
fclose(fid);
% check the file's contents:
type('iris_data2.txt')
0 个评论
更多回答(1 个)
Walter Roberson
2024-2-26
cluster_data = iniclu{i,:}; % I assuming 50 cells of 1st row is read into cluster_data when i = 1 and so on
You are doing cell array expansion there. Your command is equivalent to
[cluster_data, ~, ~, ~, ~, ~, ~, ~, ~, ~, .... ~] = iniclu{i,:}
You want something closer to
cluster_data = cell2mat(iniclu(i, :));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!