Processing cell arrays as a batch instead of one by one

1 visualización (últimos 30 días)
NALLARASU KRISH
NALLARASU KRISH el 26 de Feb. de 2024
Respondida: Voss el 26 de Feb. de 2024
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!

Respuesta aceptada

Voss
Voss el 26 de Feb. de 2024
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

Más respuestas (1)

Walter Roberson
Walter Roberson el 26 de Feb. de 2024
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, :));

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by