Saving results of a for loop in a cell array
Show older comments
Hi. I have a cell array (DataFrame) that has 16 cell, each containing an array with different length (I have attached a picture). I am trying to loop through each individual array and remove the columns (from the second column to end) that contain values more than 20 (using cell fucntion), and save the result of each loop to a new cell array (NewCell).
I have wrote this for loop which works but it seems like it is only going through the first array.
How would I go about fixing that?
Any help is much appreciated.
NewCell = cell(1,numel(DataFrame)); %creat an empty cell for the results
for iAnimal = 1:size(DataFrame) %repeat for each cell in DataFrame (1:16)
currAnimal = DataFrame {iAnimal}; %access the array inside each cell in DataFrame
G = cellfun(@(x) isnumeric(x) && x>20 , currAnimal(:,2:end)); %find values bigger than 20 in second column to the last column
F=any(G) % index columns that have a value bigger than 20 (index 1)
F(1,1)=0 % set the index for the first column as 0 since we didn't consider it in G and we want to keep every array's first column
currAnimal(:,F)= []; % remove columns from the current array
for y = 1:size(DataFrame);
NewCell{y}=currAnimal
end
end

Accepted Answer
More Answers (1)
Anton Kogios
on 23 Mar 2023
Edited: Anton Kogios
on 23 Mar 2023
It is hard to run your code without your data, but a potential source of error may be when you use 'for iAnimal = 1:size(DataFrame)'. size(DataFrame) will return [1,16], and consequently 1:[1:16] will return i = 1. Hence why you are only getting it to go through the first array.
I suggest changing it to
for iAnimal = 1:length(DataFrame)
and
for y = 1:length(DataFrame)
Categories
Find more on Loops and Conditional Statements 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!