if number is already recorded don't record

2 次查看(过去 30 天)
C.G.
C.G.2021-1-22
编辑: C.G. ,2021-1-25
My code reads data from multiple csv files, and identifies how many balls move past a certain y coordinate in each time step and stores this in a new variable 'grains'.
This is currently cumulative, so any ball with y coordinates <-0.13 is recorded in each time step even if they were recorded previously.
I want to adapt this code, to only record if the ball coordinates are <-0.13 and the particle ID hasnt already been recorded in a previous time step. I know I need to add something to the if loop but I am unsure how to write this.
Could anybody provide me with a way to do this?
%% input
%input data from each .csv file into matlab
%need particle id, velocity and coordinates stored
files = dir('*.csv'); %dir list s 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
%% particles leaving the rice pile
%for each .csv file (cell in the array), calculate the number of particles
%after a between certain y coordinates
ymax = -0.13;
for b = 1:length(particledata)
%save all the rows in the 6th column (y-coordinates) of each cell as a new variable y
y{b} = particledata{b}(:,6);
%save all the rows in the 1st column (particle ID) of each cell as a new variable id
id{b} = particledata{b} (:,1);
%use the function table2array to turn the format of the data from a table to an array
y_array{b} = table2array(y{b});
%for all the rows in the array, if the x coordinate goes beyond the outlet
%of the rice pile, display 'grain left rice pile'
if y_array{b}<ymax
disp('grain left rice pile');
end
%sum the total number of grains leaving the rice pile in each cell, and save into a new variable 'grains'
grains{b} = sum(y_array{b}<ymax);
fprintf('A total of %d grains left the rice pile\n',grains{b});
end
%% plots for just 1 power spectra for 1 experiment
%number of csv files = number of time steps the model is run for (seconds)
totalruntime = num_files;
time = 1:totalruntime;
%convert the cell file to a double file to allow plotting of data
grains = cell2mat(grains);
%mass efflux vs. time
%plot 'grains left rice pile' vs. time step as a bar graph
%0.01 sets the width of each bar
figure(1)
bar(time,grains, 0.01, 'EdgeColor', 'r')
title('mass efflux')
xlabel('Time (S)')
ylabel('Mass Efflux (No. of Particles)')

回答(1 个)

Superficial
Superficial 2021-1-22
If I've not misread the situation, it's just a simple subtraction isn't it?
newgrains at timestep10= total number of grains at timestep10 - total number of grains at timestep9
newgrains=zeros(1, length(grains));
for i=2:length(grains)
newgrains(i)=grains(i)-grains(i-1)
end
You could probably put that inside your 'for' loop if you wanted, but it should work OK (just a bit slower) at the end of your code.
Alternatively, you can look at using the histogram instead of bar plot.
  1 个评论
C.G.
C.G. 2021-1-25
I have given each particle an ID number.
I want the script to 1. identify when the grain coordinates are <-0.13, and 2. only store this number in 'grains' if the particle Id is not already recorded.
ymax = -0.13;
for b = 1:length(particledata)
%save all the rows in the 1st and 6th column (ID and y-coordinates) of each cell as a new variable y
y{b} = particledata{b}(:,[1 6]);
%use the function table2array to turn the format of the data from a table to an array
y_array{b} = table2array(y{b});
%for all the rows in the array, if the x coordinate goes beyond the outlet
%of the rice pile, display 'grain left rice pile'
if y_array{b}(:,2)<ymax
disp('grain left rice pile');
end
%sum the total number of grains leaving the rice pile in each cell, and save into a new variable 'grains'
grains{b} = sum(y_array{b}(:,2)<ymax);
fprintf('A total of %d grains left the rice pile\n',grains{b});
end

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by