# Storing logical outputs while in loop

1 view (last 30 days)
Kieran Fung on 3 Dec 2020
The entirety of my code maps out surface contact connections between a cluster of particles from an imported .txt file. The code has been written to determine connections between any amount of particles considered, where one particle is logically compared to the rest, where the code then loops through and compares the next particle to the rest in the batch.
The logic I have written ultimately determines if two particles are in contact with one another based on:
i) If the absolute value of the sum of the two respective particles radii minus the distance between them is less than a determined tolerance (to account for lack of sig. figs.), there's a surface connection
ii) If the absolute value difference between the particles and difference is greater than the tolerance, the particles are determined to be too far apart to have a connection and there is no plot determined.
What I want:
I want to store the connection vectors between all the particles, in say a cell array. This information perains to:
xplot = [particle_xyz(ii,1)*ones(connections(ii),1), particle_xyz(distance_logic(ii,:) ,1)];
yplot = [particle_xyz(ii,2)*ones(connections(ii),1), particle_xyz(distance_logic(ii,:) ,2)];
zplot = [particle_xyz(ii,3)*ones(connections(ii),1), particle_xyz(distance_logic(ii,:) ,3)];
However, sometimes the connection logic finds that there is not a connection, and indexes the result as [ ], causing matrix of information on the certain loop value to go to zero.
Any ideas on how to store this information without this occuring? The code can be seen below.
%%
clear all
close all
% Particle Network Structure - Yade Cylinder
% Import data into cells
fileID = fopen('timeStep_100_1.txt');
data_import = textscan(fileID, '%f %f %f %f %f %f %s');
fclose(fileID);
% Import data legend
% data_import{1} = particle ID
% data_import{2} = x coordinates
% data_import{3} = y coordinates
% data_import{4} = z coordinates
% data_import{5} = z_velocity
% data_import{6} = r_particle
% data_import{7} = force vector
particle_xyz = [data_import{2}, data_import{3}, data_import{4}];
% Particle mapping
num_particle = cellstr(num2str([1:length(particle_xyz)]'));
figure(1); clf
scatter3( particle_xyz(:,1), particle_xyz(:,2), particle_xyz(:,3), 'r', 'filled', 'linewidth', 30)
hold on
text(particle_xyz(:,1)+0.01, particle_xyz(:,2)+0.01, particle_xyz(:,3)+0.01, num_particle);
% aa = [1:25]'; bb = num2str(aa); cc = cellstr(bb);
% Mapping logic
tol = r_avg/100;
N = length(particle_xyz)
% ii = number of particles to be mapped
for ii = 1:N
% Connectivity Mapping
distance(ii,:) = ( (particle_xyz(ii,1) - particle_xyz(:,1)).^2 + (particle_xyz(ii,2) - particle_xyz(:,2)).^2 + ...
(particle_xyz(ii,3) - particle_xyz(:,3)).^2 ).^0.5;
distance_logic(ii,:) = abs(R(ii,:) - distance(ii,:)) < tol;
% Total connections per particle
connections(ii,:) = sum(distance_logic(ii,:));
xplot = [particle_xyz(ii,1)*ones(connections(ii),1), particle_xyz(distance_logic(ii,:) ,1)];
yplot = [particle_xyz(ii,2)*ones(connections(ii),1), particle_xyz(distance_logic(ii,:) ,2)];
zplot = [particle_xyz(ii,3)*ones(connections(ii),1), particle_xyz(distance_logic(ii,:) ,3)];
plot3(xplot', yplot', zplot', 'k')
hold on
end
%scatter(vect_end_x',vect_end_y');
figure(1); xlabel('x position','fontsize', 18); ylabel('y position','fontsize', 18); zlabel('z position','fontsize', 18); ...
title('Yade data simulation: 20 Particles', 'fontsize', 18)
ax1 = gca; % ability to modify axes
ax1.FontSize = 18
scatter3( particle_xyz(:,1), particle_xyz(:,2), particle_xyz(:,3), 'r', 'linewidth', 3)