Page 118 - Basics of MATLAB and Beyond
P. 118

fid = fopen(’asc.dat’);
                               The fopen command returns a file identifier, fid, which is an integer
                               that must be used as the first argument of any subsequent file-reading
                               or file-writing function that uses the file asc.dat. If the file cannot be
                               opened (for example, if it does not exist or exists in a directory that
                               cannot be found by matlab), then a value fid=-1 is returned. Once
                               the file is opened the data can be read using the following command:

                               >> a = fscanf(fid,’%d/%d %d:%d:%d %g%g%g%g%g%g’);
                               >> size(a)
                               ans  =
                                   55     1
                               The fscanf command has read in all the data up to the end of the file.
                               In the file are 11 numbers per line (2 numbers in the date, plus 3 in the
                               time, plus 6 other numbers), and there are 5 lines, for a total of 55 data
                               values; these have been read into a column-vector called a. The format
                               string ‘%d/%d %d:%d:%d %g%g%g%g%g%g’ means “look for two decimal
                               numbers separated by slashes, skip some whitespace, then look for three
                               decimal numbers separated by colons, skip some more whitespace, then
                               look for six general format floating point numbers” (see the section on
                               string conversion on page 79). fscanf reads in such numbers until the
                               end of the file, or you can put in a parameter to read in a certain number
                               of values. We only now need to reshape the vector a to a matrix having
                               11 columns
                               N = length(a)/11;
                               a = reshape(a,11,N)’;
                               The date and time values are in the first five columns:
                               >> a(:,1:5)
                               ans  =
                                   10     6     11    18     0
                                   10     6     11    18     1
                                   10     6     11    18     2
                                   10     6     11    18     3
                                   10     6     11    18     4
                               And the remaining values are

                               >> a(:,6:11)
                               ans  =
                                -34.8550  151.3057   216.4000   70.9100   -61.2300    0.2900
                                -34.8555  151.3065   214.8000   71.3800   -60.8000   -0.8800
                                -34.8561  151.3073   212.7000   71.8600   -60.6400   -1.6400
                                -34.8566  151.3081   210.8000   72.4000   -60.3500   -1.6700
                                -34.8572  151.3089   209.7000   72.8300   -60.0600   -1.3300



                               c   2000 by CRC Press LLC
   113   114   115   116   117   118   119   120   121   122   123