Geoscience Reference
In-Depth Information
the function
textscan
to perform this task. h e MATLAB workspace should
i rst be cleared by typing
clear
at er the prompt in the Command Window. MATLAB can now import the
data from the i le
geochem.txt
using the
textscan
command.
fid = fopen('geochem.txt');
C = textscan(fid,'%u %f %f','Headerlines',1,'CollectOutput',1);
fclose(fid);
h is script opens the i le
geochem.txt
for
read only
access using
fopen
and
dei nes the i le identii er
fid
, which is then used to read the text from the
i le using
textscan
and to write it into the cell array
C
. h e character string
%u
%f %f
dei nes the conversion specii ers enclosed in single quotation marks,
where
%u
stands for the 32-bit unsigned integer output class and
%f
stands for
a 64-bit double-precision l ointing-point number. h eparameter
Headerlines
is set to 1, which means that a single header line is ignored while reading the
i le. If the parameter
CollectOutput
is 1 (i.e., is true),
textscan
concatenates
output cells with the same data type into a single array. h e function
fclose
closes the i le dei ned by
fid
. h e array
C
is a cell array, which is a data type
with indexed containers called cells (see Section 2.5). h e advantage of this
data type is that it can store data of various types and sizes, such as character
strings, double-precision numbers, and images in a single variable such as
C
. Typing
C
yields
C =
[6x1 uint32] [6x2 double]
indicating that
C
contains a 6-by-1 32-bit unsigned integer array, which is
the sample ID, and a 6-by-1 double-precision array, which represents the
percentages of carbon and sulfur in each sample. We can access the contents
of the cells in
C
by typing
data1 = C{1}
data2 = C{2}
which yields
data1 =
101
102