for loop to go through a cell array

I have written a code which tells me how many particles go past a certain x-coordinate for a single time step. The code accesses one cell of a 710x1 cell array currently, but now I want to tell it to do this for all the cells in the array, so i can plot a particles vs. time graph.
I know I need another for loop to do this, but I am unsure how to tell it to go through all the cells in the array.
%% particles leaving the rice pile
%for each cell in the array (timestep), calculate the number of particles after a certain x coordinate
%access the {nth} cell of the cell array, and save all the rows in the 5th column (x-coordinates) as a new variable
xc = particledata{212}(:,5);
%use the function table2array to turn the format of the data from a table to an array
xc_array = table2array(xc);
%for all the rows in the array, if the x coordinate goes beyond the outlet
%of the rice pile, display 'grain left rice pile'
%-0.15 is the x coordinate for the outlet
ricepileoutlet = -0.15
for b = 1:size(xc_array,1)
if xc_array(b,1)< ricepileoutlet
disp('grain left rice pile')
end
end
%display how many grains in total left the rice pile in the time step
%add up the number of times the x coordinate is >0.188 and save this in a new variable 'grains'
grains=sum(xc_array(:,1)< ricepileoutlet);
fprintf('A total of %d grains left the rice pile\n',grains);

Answers (1)

No loop needed here
% FOllowing result counts the total grain left rice pile
result=sum(xc_array>ricepileoutlet)
Note: Assumed that xc_array is an 1D array

7 Comments

I agree that the total grains leaving are displayed, but surely this is only for the 1 cell in the array which I have asked it to look at when i create xc_array?
I have a cell array which is 710x1. Each cell in that array contains 1 seconds worth of information.
Therefore I want the code to tell me how many grains leave in each cell of the array to create a bar graph which shows how many grains leave the model each second.
It would be more easier to answer, if you provider the size of xc_array. If it is 2D and comparing individual elemnets with thrshold value (ricepileoutlet), then also
data=xc_array>ricepileoutlet;
result=sum(data(:))
xc_array is not a cell array?
table2array(T) converts the table, T, to a homogeneous array, A.
if the data in cell array as you commented, let suppose xc_array as a cell array
grain_num=zeros(1,size(xc_array,1))
for i=1:size(xc_array,1)
data=xc_array{i};
grain_num(i)=sum(data>ricepileoutlet)
end
bar(grain_num)
I will share my whole code below.
I started with 710 .csv files which I inputted to matlab. Each one of these .csv files has 1 second of data in it.
%% input
%input data from each .csv file into matlab
%need particle id, velocity and coordinates stored
files = dir('*.csv'); %dir lists the files in a folder. In the specified folder, recognise all the .csv files
num_files = length(files); %specify how many files have been found and call it num_files
particledata = cell(length(files), 1); %create a cell array the same length as the number of files in one column
%for all the files found in the specified folder, read the tables of data and fill the empty cell array 'results' with the data in each .csv file
for a = 1:num_files
particledata{a} = readtable(files(a).name);
end
%% time steps
%number of excel files = number of time steps the model is run for (seconds)
totalruntime = num_files;
%% particles leaving the rice pile
%for each cell in the array (timestep), calculate the number of particles after a certain x coordinate
%access the {nth} cell of the cell array, and save all the rows in the 5th column (x-coordinates) as a new variable
xc = particledata{600}(:,5);
%use the function table2array to turn the format of the data from a table to an array
xc_array = table2array(xc);
%for all the rows in the array, if the x coordinate goes beyond the outlet
%of the rice pile, display 'grain left rice pile'
%-0.125 is the x coordinate for the end of the box on the mfix model
ricepileoutlet = -0.15;
for c = 1:size(xc_array,1)
if xc_array(c,1)< ricepileoutlet
disp('grain left rice pile')
end
end
%display how many grains in total left the rice pile in the time step
%add up the number of times the x coordinate is >0.188 and save this in a new variable 'grains'
grains=sum(xc_array(:,1)< ricepileoutlet);
fprintf('A total of %d grains left the rice pile\n',grains);
Have you tried this example?
grain_num=zeros(1,size(xc_array,1));
for i=1:size(xc_array,1)
data=xc_array{i};
grain_num(i)=sum(data>ricepileoutlet);
end
bar(grain_num);
as you said above, xc_array is not a cell array.
my cell array containing all my 710 .csv files is 'particledata'
what I want to do overall is:
  • go through each cell of particledata and look at the data in column 5
  • for each of the cells in particle data, tell me if any of the rows in column 5 have values <-0.15, if so how many.
  • create a new variable called 'grains', which tells me how many rows have values <0.15 for each cell
  • plot a bar chart of grains vs. time

Sign in to comment.

Categories

Tags

Asked:

on 25 Nov 2020

Commented:

on 25 Nov 2020

Community Treasure Hunt

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

Start Hunting!