MATLAB Import Data

From EVOCD
(Difference between revisions)
Jump to: navigation, search
(Created page with '== Abstract == This example shows how to import the date from data file to MATLAB. The file with information about positions of the atoms is given and the objective is to extract…')
 
Line 4: Line 4:
 
Author(s): [http://www.hpc.msstate.edu/directory/information.php?eid=1967 Mark A. Tschopp], Dmitry I. Zhuk
 
Author(s): [http://www.hpc.msstate.edu/directory/information.php?eid=1967 Mark A. Tschopp], Dmitry I. Zhuk
  
== MATLAB Input ==
+
== Input File ==
 +
 
 
File of some formatting is given and the objective is to get a certain data from it, discharging all the other information.
 
File of some formatting is given and the objective is to get a certain data from it, discharging all the other information.
 
The first 20 lines of the example is shown below.
 
The first 20 lines of the example is shown below.
Line 29: Line 30:
 
       10  1      3.381448    36.249498      4.050122    -3.359977      0.000024
 
       10  1      3.381448    36.249498      4.050122    -3.359977      0.000024
 
       11  1      1.361451    36.329039      2.025123    -3.359969      0.000024
 
       11  1      1.361451    36.329039      2.025123    -3.359969      0.000024
 +
      ...
  
 
</pre>
 
</pre>
  
 +
From this file the useful information is number of atoms, box bounds, atom number (first column in the table), atom's x, y, z coordinates (third, forth and fifth columns correspondingly). MATLAB is used in this case get for future use this information.
 +
 +
== MATLAB Script ==
 +
 +
File processing have been done by using the following MATLAB program.
 +
 +
<pre>
 +
 +
clear all, close all;
 +
filename = some.file.to.convert.f;
 +
fid = fopen(filename);
 +
fgetl(fid);
 +
fgetl(fid);
 +
fgetl(fid);
 +
tline = fgetl(fid);
 +
a = textscan(tline, '%d');
 +
atoms = a{1};
 +
fgetl(fid);
 +
tline = fgetl(fid);
 +
a = textscan(tline,'%f %f');
 +
xlo  = a{1}; xhi = a{2};
 +
tline = fgetl(fid);
 +
a = textscan(tline,'%f %f');
 +
ylo  = a{1}; yhi = a{2};
 +
tline = fgetl(fid);
 +
a = textscan(tline,'%f %f');
 +
zlo  = a{1}; zhi = a{2};
 +
fgetl(fid);
 +
B = textscan(fid, '%d %*d %f %f %f %*f %*f', atoms);
 +
atomID = B{1};
 +
Xpos = B{2};
 +
Ypos = B{3};
 +
Zpos = B{4};
 +
fclose(fid);
 +
</pre>
 +
The following is the explanation of the script and command used.
 +
<pre>
 +
clear all, close all;
 +
filename = some.file.to.convert.f;
 +
fid = fopen(filename);
 +
<\pre>
 +
First command closes all MATLAB previously opened files and cleans the memory. This is done to make sure that none of the previous work will interrupt with the processing.
 +
Second line assigns actual file this data to variable "filename" and the second opens it with fileID "fid". This fileID will be used later to identify on which command does command have to act.
 +
<pre>
 +
<\pre>
 +
<pre>
 +
<\pre>
 +
<pre>
 +
<\pre>
 +
<pre>
 +
<\pre>
 
== Go Back ==
 
== Go Back ==
 
*[[MaterialModels: Nanoscale|Nanoscale]]
 
*[[MaterialModels: Nanoscale|Nanoscale]]

Revision as of 14:38, 8 November 2011

Abstract

This example shows how to import the date from data file to MATLAB. The file with information about positions of the atoms is given and the objective is to extract all useful information from the data file and omit the unneeded information. In this tutorial shown how to use such MATLAB functions as fgetl, textscan, fopen, fprintf and others.

Author(s): Mark A. Tschopp, Dmitry I. Zhuk

Input File

File of some formatting is given and the objective is to get a certain data from it, discharging all the other information. The first 20 lines of the example is shown below.

 ITEM: TIMESTEP
           1
 ITEM: NUMBER OF ATOMS
        5828
 ITEM: BOX BOUNDS
  0.000000000000000E+000   109.424979441872     
  0.000000000000000E+000   218.533081144200     
  0.000000000000000E+000   4.05000019073486     
 ITEM: ATOMS
       1   1     53.069549      5.989693      2.024924     -3.319741      0.153287
       2   1     52.569822      2.273330      2.024912     -3.141638     13.948742
       3   1      1.577682     42.407913      4.050134     -3.359999      0.000006
       4   1      3.599188     42.331114      2.025135     -3.360004      0.000006
       5   1      1.432760     38.355783      4.050126     -3.359985      0.000016
       6   1      3.525962     40.304560      4.050129     -3.360000      0.000010
       7   1      1.504881     40.382048      2.025129     -3.359994      0.000010
       8   1      3.453345     38.277394      2.025125     -3.359991      0.000016
       9   1      1.291089     34.301725      4.050119     -3.359945      0.000038
      10   1      3.381448     36.249498      4.050122     -3.359977      0.000024
      11   1      1.361451     36.329039      2.025123     -3.359969      0.000024
      ...

From this file the useful information is number of atoms, box bounds, atom number (first column in the table), atom's x, y, z coordinates (third, forth and fifth columns correspondingly). MATLAB is used in this case get for future use this information.

MATLAB Script

File processing have been done by using the following MATLAB program.


clear all, close all;
filename = some.file.to.convert.f;
fid = fopen(filename);
fgetl(fid);
fgetl(fid);
fgetl(fid);
tline = fgetl(fid);
a = textscan(tline, '%d');
atoms = a{1};
fgetl(fid);
tline = fgetl(fid);
a = textscan(tline,'%f %f');
xlo  = a{1}; xhi = a{2};
tline = fgetl(fid);
a = textscan(tline,'%f %f');
ylo  = a{1}; yhi = a{2};
tline = fgetl(fid);
a = textscan(tline,'%f %f');
zlo  = a{1}; zhi = a{2};
fgetl(fid);
B = textscan(fid, '%d %*d %f %f %f %*f %*f', atoms);
atomID = B{1};
Xpos = B{2};
Ypos = B{3};
Zpos = B{4};
fclose(fid);

The following is the explanation of the script and command used.

clear all, close all;
filename = some.file.to.convert.f;
fid = fopen(filename);
<\pre>
First command closes all MATLAB previously opened files and cleans the memory. This is done to make sure that none of the previous work will interrupt with the processing.
Second line assigns actual file this data to variable "filename" and the second opens it with fileID "fid". This fileID will be used later to identify on which command does command have to act.
<pre>
<\pre>
<pre>
<\pre>
<pre>
<\pre>
<pre>
<\pre>
== Go Back ==
*[[MaterialModels: Nanoscale|Nanoscale]]
Personal tools
Namespaces

Variants
Actions
home
Materials
Material Models
Design
Resources
Projects
Education
Toolbox