Plotting two 3D points from a textscan output

1 view (last 30 days)
I have the following text file:
Eu3+ 1
10.06037350 -4.673610300 -1.834337367
1.22604929765 -2.02696902730 0.734136756877
10517.3113705 -9795.46057045 -2441.96899290
Eu3+ 2
-11.25268764 3.982158778 -4.411302032
0.239696547775E-01 0.719865908056 0.654664578760
-3423.27694546 -2308.86356341 -348.027397200
Whereby the entries are to be considered as (1) atom type (2) atom number (3)(4)(5) xyz coordinates (6)(7)(8)(9)(10)(11) irrelevant, and then repeated. I want to be able to represent a huge list of such data entries as atom positions and ultimately perform further calculations upon them, but currently I am only able to plot a single atom, using this code:
fid = fopen('twoatom.txt','r'); %read as a single cell
A = textscan(fid,'%s'); %perform textscan
A = A{1,1}(1:5); %Attain data as 1x5 cell array
a1 = A(1,:); %Atom type to vector
a2=str2double(A(2:end,:)); %Atom coordinates to vector
scatter3(a2(2),a2(3),a2(4))
But I have already had to ask for quite a bit of help to get this far, and am struggling to extend the code to accommodate a second atom. Could anyone enlighten me as to how this would be done? I am hoping that if someone can I will be able to extend the code myself to accommodate every atom in the file.
Any help would be greatly appreciated.
Kind regards,
Tom

Accepted Answer

sixwwwwww
sixwwwwww on 28 Oct 2013
Dear Tom, you can do it as follows:
ID = fopen('filename.txt', 'r');
data = textscan(ID, '%s');
fclose(ID);
data = data{:};
atom_type = data(1:11:length(data));
atom_number = str2double(data(2:11:length(data)));
count = 1;
for i = 3:11:length(data)
xyz(count, :) = str2double(data(i:i+2));
count = count + 1;
end
scatter3(xyz(:,1), xyz(:,2), xyz(:,3));
I hope it helps. Good luck!
  6 Comments
Tom
Tom on 29 Oct 2013
Ah, I checked data between steps and I see what you are saying. Excellent stuff. I remember the usage of colons in defining vectors, but I hadn't encountered it in the first situation you described before. I have a complex task ahead of me after a long period away from Matlab, perhaps I might ask for your guidance again if you don't mind :)
Kind regards,
Tom

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!