CERI7104CIVL8126 Data Analysis in Geophysics Continue Introduction to

  • Slides: 16
Download presentation
CERI-7104/CIVL-8126 Data Analysis in Geophysics Continue Introduction to Matlab Character I/O (fscanf, textscan, fgetl)

CERI-7104/CIVL-8126 Data Analysis in Geophysics Continue Introduction to Matlab Character I/O (fscanf, textscan, fgetl) type command Graphics Handles clf, clear, delete Lab – 8, 09/19/19

Reading in data load – Have seen, reads arrays of binary numeric data or

Reading in data load – Have seen, reads arrays of binary numeric data or ascii numeric data ONLY. Typical input file does not meet these conditions. First of several methods A = fscanf(file. ID, format. Spec) Examples in matlab_fscanf_ex. m

Reading in data The example files here all numeric or we ignore text. https:

Reading in data The example files here all numeric or we ignore text. https: //www. mathworks. com/help/matlab/ref/fscanf. html The structure of the files is known and described by the format. Spec.

format. Spec – describes the data. chr = '0. 41 8. 24 3. 57

format. Spec – describes the data. chr = '0. 41 8. 24 3. 57 6. 24 9. 27; ’ C = textscan(chr, '%f; (' Here using variable instead of file. ID (takes string contents of variable as input line from file – known as “internal read”). Format specification is the %f for floating point. When a format spec runs out – it repeats from the beginning.

<<chr = '0. 41 8. 24 3. 57 6. 24 9. 27; ' <<C

<<chr = '0. 41 8. 24 3. 57 6. 24 9. 27; ' <<C = textscan(chr, '%f; (' <<whos C Name Size Bytes Class C 1 x 1 152 cell <<chr = "0. 41 8. 24 3. 57 6. 24 9. 27; " <<C = textscan(chr, '%f; (' <<whos C Name Size Bytes Class C 1 x 1 152 cell <<C C= 1× 1 cell array 1× 5} double{ ] <<C[{: } ans= 0. 410000000 8. 240000000 3. 570000000 6. 240000000 9. 270000000 Attributes

Example of particularly difficult file to read – GPS RINEX file. . . Has

Example of particularly difficult file to read – GPS RINEX file. . . Has a header full of metadata id of header info 30. 0000 INTERVAL. . . END OF HEADER 16 1 1 0 0 0. 0000000 0 10 G 15 G 27 G 22 G 30 G 28 G 13 G 18 G 10 G 11 G 08 data desc -14625784. 02147 -11358678. 40846 22561784. 4224 22561780. 0784 data 36. 2504 46. 5004 data. . . 9 more of these (18 lines). . . 16 1 1 0 0 30. 0000000 0 11 G 15 G 01 G 27 G 22 G 30 G 28 G 13 G 18 G 10 G 11 G 08. . . 11 observations (22 lines). . . 15 0000. comment in middle INTERVAL 16 1 1 0 0 45. 0000000 0 10 G 15 G 01 G 27 G 22 G 30 G 28 G 13 G 18 G 10 G 08

Format Speification Describes the input fields – number or alpha, format and size of

Format Speification Describes the input fields – number or alpha, format and size of number (as opposed to “free format” where things are separated by spaces and the computer figures out what it is). Does not fit on powerpoints Go to web page https: //www. mathworks. com/help/matlab/ref/fscanf. html See also https: //www. mathworks. com/help/matlab_prog/formatting-strings. html

Some additional features of Matlab Ability to treat date and time as a “regular”

Some additional features of Matlab Ability to treat date and time as a “regular” vector for plotting >> C=datetime('2009 -12 -29 11: 47: 34. 96') Error using datetime (line 635) Could not recognize the date/time format of '2009 -12 -29 11: 47: 34. 96'. You can specify a format using the 'Input. Format' parameter. If the date/time text contains day, month, or time zone names in a language foreign to the 'en_US' locale, those might not be recognized. You can specify a different locale using the 'Locale' parameter.

>> help datetime Arrays to represent dates and times. datetime arrays store values that

>> help datetime Arrays to represent dates and times. datetime arrays store values that represent points in time, including a date and a time of day. Use the datetime constructor to create an array of datetimes from strings, character vectors, or from vectors of date/time components. Use datetime('now'), datetime('today'), datetime('yesterday'), or datetime('tomorrow') to create scalar datetimes at or around the current moment. Goes on for several screens.

Somewhere on those screens (hopefully in an example) you will find how to specify

Somewhere on those screens (hopefully in an example) you will find how to specify the format. <<datetime. set. Default. Formats('default', 'yyyy-MM-dd hh: mm: ss(' <<C=datetime("2009 -12 -29 11: 47: 34. 96(" C= datetime 2009 -12 -29 11: 47: 34 I actually found this at. https: //www. mathworks. com/help/matlab_prog/set-display-format-ofdate-and-time-arrays. html

Write 2 programs to read the files mixedin 1. dat and mixedin 2. dat

Write 2 programs to read the files mixedin 1. dat and mixedin 2. dat These files have earthquake data. Most earthquake data files have a mixture of numbers and text (the first file) and time formatted data, and a header (that typically identifies the data columns (the second file).

textscan Works pretty much like fscanf https: //www. mathworks. com/help/matlab/ref/textscan. ht ml Reads into

textscan Works pretty much like fscanf https: //www. mathworks. com/help/matlab/ref/textscan. ht ml Reads into cell array.

Can do simple “edits” on data with format. Spec Read the same character vector,

Can do simple “edits” on data with format. Spec Read the same character vector, and truncate each value to one decimal digit. C = textscan(chr, '%3. 1 f %*1 d'); The specifier %3. 1 f indicates a field width of 3 digits and a precision of 1. The textscan function reads a total of 3 digits, including the decimal point and the 1 digit after the decimal point. The specifier, %*1 d, tells textscan to skip the remaining digit.

file. ID = fopen(‘scan 1. dat’); C = textscan(file. ID, '%s %s %f 32

file. ID = fopen(‘scan 1. dat’); C = textscan(file. ID, '%s %s %f 32 %d 8 %u %f %f %s %f'); fclose(file. ID); whos C More (poorly documented) format specifiers (32 is bits – single precision float, 8 is b 1 ts - 1 byte integer). C=1× 9 cell Columns 1 through 5 {3 x 1 cell} {3 x 1 single} {3 x 1 int 8} {3 x 1 uint 32} Columns 6 through 9 {3 x 1 double} {3 x 1 cell} {3 x 1 double} several more examples on this page

Fgetl Go directly to the mathworks web page for it (how you will learn

Fgetl Go directly to the mathworks web page for it (how you will learn about most stuff anyway) https: //www. mathworks. com/help/instrument/fgetl. html Read line of text from instrument and discard terminator. For hooking up to an digital instrument.

type command - displays file in command window Graphics Handles – from “Getting a

type command - displays file in command window Graphics Handles – from “Getting a handle on Matlab Graphics” by Roger A. Green. clf, clear, delete