Processing cell arrays as a batch instead of one by one

조회 수: 1 (최근 30일)
NALLARASU KRISH
NALLARASU KRISH 2024년 2월 26일
답변: Voss 2024년 2월 26일
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!

채택된 답변

Voss
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]}
iniclu = 2×2 cell array
{[ 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')
1.000000,2.000000,3.000000,4.000000 5.000000,6.000000,7.000000,8.000000 9.000000,10.000000,11.000000,12.000000 13.000000,14.000000,15.000000,16.000000

추가 답변 (1개)

Walter Roberson
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, :));

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by