A11 - Reading data files using the load() and textread() commands


A11A.m - Reading numbers using the load() command

This example shows how to read in a data file planetsA.txt containing two columns of numbers. The first column contains the average distance of a planet to the sun (measured in units of the Earth-Sun distance) and the second column contains the orbital period of the planet measured in Earth years. The rows are not labled (they will be discussed below).

0.3871  0.2408
0.7233	0.6152
1.0     1.0
1.5236	1.8808
2.767	4.603
5.204	11.862
9.583	29.457
19.201	84.012
30.048	164.79
39.482	247.68
43.132	283.28
45.791	309.88
67.89	559

Because this file contains only numbers and has the same number of rows and columns throughout the file (i.e. each row has exactly two entries), we can use the load() command. In the example below, we read in the data file planetsA.txt and store its contents in a matrix we call data.

data = load('planetsA.txt');

% extract the semi-major axis from the first column of planetsA
a = data(:,1);

% extract the period from the second column of planetsA
P = data(:,2);

% First print a header labeling the columns
fprintf('Planet #    semi-major axis (AU)    period (years)\n')

% now print the data. Note the use of %6.2f formatting. The number to the
% right of the decimal represents the number of decimal places, and the
% number to the left of the decimal point represents the total number of
% spaces reserved for the number.
for i=1:length(a)
    fprintf('%5i             %6.2f               %6.2f\n',i,a(i),P(i));

Download A11.m

Download the data file planetsA.txt

The command a = planetsA(:,1) pulls out the first column of numbers and stores them in a 1D array called a, which is the symbol commonly used for the semi-major axis (i.e. the average distance to the Sun). Similarly, the next line in the code pulls out the periods stored in the second column.

Finally, the data in the file are printed to the screen in such a way that the numbers are lined up neatly. Because the number of digits in the numbers vary, we must specify the amount of space we want to reserve to print each number. We do this with the number to the left of the decimal point in the fprintf formating specification. The 6 in the %6.2f specificaiton says to reserve 6 spaces for the number. To understand what this does, try just deleting the 6 and rerunning the program. Or try replacing the 6 with another number (try 2 or 12, for example).

Output of the program:

Planet #    semi-major axis (AU)    period (years)
    1               0.39                 0.24
    2               0.72                 0.62
    3               1.00                 1.00
    4               1.52                 1.88
    5               2.77                 4.60
    6               5.20                11.86
    7               9.58                29.46
    8              19.20                84.01
    9              30.05               164.79
   10              39.48               247.68
   11              43.13               283.28
   12              45.79               309.88
   13              67.89               559.00

A11B.m - Reading numbers and strings using the textread() command

Tthe load() command only works when the file to be read contains numbers with a fixed number of rows and columns. To read in a data file contaning both text and numbers, or a file that has a variable number of columns, we must use the textread() command. The data file planetsB.txt contains the same information as planetsB.txt, but it also contains the name of each planet (or dwarf planet). In addition, the first row contains header information that describes what each column represents. Here's the file:

PLANET          a(AU)   P(years)
Mercury		0.3871	0.2408
Venus		0.7233	0.6152
Earth		1.0     1.0
Ceres		2.767	4.603
Mars		1.5236	1.8808
Jupiter		5.204	11.862
Saturn		9.583	29.457
Uranus		19.201	84.012
Neptune		30.048	164.79
Pluto		39.482	247.68
Haumea		43.132	283.28
Makemake	45.791	309.88
Eris		67.89	559

When using the textread() command we must specify the formatting of the data file. We use '%s' to read the names of the planets and %f to read the numeric data. Also notice that the output of the textread() command is stored directly into three arrays whose names we specify as planet, a, and P. The last argument of the textread() command specifies the number of header lines that should be skipped over. Our file contains one header line that we want to skip over, so we pass a 1. Heres the program:

[planet, a, P] = textread('planetsB.txt', '%s %f %f', 'headerlines', 1);

% First print a header labeling the columns
fprintf('Planet        semi-major axis (AU)    period (years)\n')

% now print the data. The planet names are stored as cells rather than
% strings, so we need convert each planet name to a string using the char()
% command. Note also that we use %-10s to format the planet names. The '-'
% sign will left justify the names (the default is right-justified).
for i=1:length(a)
     fprintf('%-10s           %6.2f               %6.2f\n',char(planet(i)),a(i),P(i));

Download L2ex11B.m

Download the data file planetsB.txt

Notice that when we print the name of the planet we specify '%-10s'. The minus sign '-' is used to left-justify the names. Removing the minus sign would default to right-justification (try it and see).

Output of the program:

Planet        semi-major axis (AU)    period (years)
Mercury                0.39                 0.24
Venus                  0.72                 0.62
Earth                  1.00                 1.00
Ceres                  2.77                 4.60
Mars                   1.52                 1.88
Jupiter                5.20                11.86
Saturn                 9.58                29.46
Uranus                19.20                84.01
Neptune               30.05               164.79
Pluto                 39.48               247.68
Haumea                43.13               283.28
Makemake              45.79               309.88
Eris                  67.89               559.00

Monkey Home   |    Prev   |   Next