for loop to go through a cell array

조회 수: 2 (최근 30일)
C.G.
C.G. 2020년 11월 25일
댓글: C.G. 2020년 11월 25일
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);

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 11월 25일
편집: KALYAN ACHARJYA 2020년 11월 25일
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
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 11월 25일
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);
C.G.
C.G. 2020년 11월 25일
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

댓글을 달려면 로그인하십시오.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by