Processing cell arrays as a batch instead of one by one
    2 views (last 30 days)
  
       Show older comments
    
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 Comments
Accepted Answer
  Voss
      
      
 on 26 Feb 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]}
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 Comments
More Answers (1)
  Walter Roberson
      
      
 on 26 Feb 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, :));
0 Comments
See Also
Categories
				Find more on Matrix Indexing in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

