if number is already recorded don't record
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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)')
0 comentarios
Respuestas (1)
Superficial
el 22 de En. de 2021
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 comentario
Ver también
Categorías
Más información sobre Operators and Elementary Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!