I want to plot a xyz graph and also get clusters from the array of my data; can someone help please. Atoms are linked by indices. (attaching the sheet for reference)
1 view (last 30 days)
Show older comments
I have combined the information from the bonds file and xyz file and want to plot a graph with various clusters (based on how the indices are connected). Is there a code to get this done with?
3 Comments
Answers (1)
Chetan
on 2 May 2024
I understand you wish to plot a 3D graph for clusters from an array of data. To visualize a 3D graph of atoms and identify clusters based on the connectivity defined by indices, MATLAB's graph and clustering functions can be utilized.
Step 1: Import Data
filename = 'Testfile3.xlsx'; % Update this with the actual file name
data = readtable(filename);
Step 2: Create Graph
Assuming the `id` columns represent the atom indices and `id_1` to `id_5` signify connections (with zeros indicating no connections), you can construct an edge list and create a graph.
% Initialize an empty array for edges
edges = [];
% Assuming 'id_of_atom' is the unique identifier for atoms
% and connections are in 'id_1' to 'id_5' columns
for i = 1:30
atomId = data.idOfAtom(i);
for j = 11:15 % Assuming id_1 to id_5 are in columns 11 to 15
connectedAtomId = data{i,j};
if ~isnan(connectedAtomId) % Check if there is a connection
edges = [edges; atomId, connectedAtomId];
end
end
end
% Create the graph
G = graph(edges(:,1),edges(:,2));
Step 3: Plotting
To plot the graph in 3D, the x, y, and z coordinates of each atom are required.
figure;
hold on;
% Plot edges
for i = 1:size(edges,1)
atomIndex1 = find(data.idOfAtom == edges(i,1));
atomIndex2 = find(data.idOfAtom == edges(i,2));
x = [data.x_axis(atomIndex1); data.x_axis(atomIndex2)];
y = [data.y_axis(atomIndex1); data.y_axis(atomIndex2)];
z = [data.z_axis(atomIndex1); data.z_axis(atomIndex2)];
plot3(x, y, z, 'k-'); % 'k-' for black lines
end
% Plot atoms on top of the edges
scatter3(data.x_axis, data.y_axis, data.z_axis, 36, 'filled');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Graph of Atoms and Bonds');
hold off;
Step 4: Clustering
To identify clusters based on connectivity:
% Find connected components (clusters)
clusters = conncomp(G);
% Number of clusters
numClusters = max(clusters);
% Display number of clusters
disp(['Number of clusters: ', num2str(numClusters)]);
For further information, refer to the following documentation:
- https://www.mathworks.com/help/matlab/ref/readtable.html
- https://www.mathworks.com/help/matlab/ref/graph.htm
- https://www.mathworks.com/help/matlab/ref/graph.conncomp.html
Thanks
Chetan
0 Comments
See Also
Categories
Find more on Directed Graphs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!