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!