Plotting two 3D points from a textscan output

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

 Réponse acceptée

sixwwwwww
sixwwwwww le 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 commentaires

Tom
Tom le 29 Oct 2013
Hi sixww...
Thanks for your response, I am Matblabless for the next fews hours but I look forward to giving that a try! I may have a few questions for you, if you wouldn't mind answering them I would have to buy you a steak!
Kind regards,
Tom
sixwwwwww
sixwwwwww le 29 Oct 2013
You are welcome. Yes you can ask questions. If i will be able to help you then I will surely help you
Tom
Tom le 29 Oct 2013
sixww..
This really is exactly what I needed, I cannot thank you enough! Would you mind explaining to me however, what is the purpose of the line: data = data{:}; ? Also, what is the purpose of colons ':' generally?
Again, thankyou thankyou thankyou!
Tom
As you can see 'data' in line 2 of the code is one cell which contains all the values read from the text file and this 'data' in line 2 further has cells which contain actual data separated by white space delimination. so i used
data = data{:};
to unwrap those cells for further processing. You can use break points in the code and can see data at each step.
Basically ':' is used for vectorization in MATLAB. The purpose of colon can be explained with the help of above code as:
data = data{:}; % here ':' means all the elements in the cell 'data'
1:11:length(data) % here ':' is used to make an array starting from 1 and incrementing 11 till the length of vector
I hope it explains you some how the purpose of ':'. You are welcome if you can't understand anything further
Tom
Tom le 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
sixwwwwww
sixwwwwww le 29 Oct 2013
You are welcome anytime

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by