CERI-7104/CIVL-8126 Data Analysis in Geophysics
Description: CERI-7104CIVL-8126 Data Analysis in Geophysics Continue Introduction to Matlab Character IO (fscanf, textscan, fgetl) type command Graphics Handles clf, clear, delete Lab 8, 091919 Reading in data load Have seen, reads arrays of
Related Topics
Download Presentation
"CERI-7104/CIVL-8126 Data Analysis in Geophysics" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
slide1. 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<br>
slide2. 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(fileID,formatSpec)
Examples in matlab_fscanf_ex.m<br>
slide3. Reading in data
The example files here are 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 formatSpec.<br>
slide4. formatSpec – describes the data.
chr = '0.41 8.24 3.57 6.24 9.27’;
C = textscan(chr,'%f');
Here using variable instead of fileID (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.<br>
slide5. >> chr = '0.41 8.24 3.57 6.24 9.27';
>> C = textscan(chr,'%f');
>> whos C
Name Size Bytes Class Attributes
C 1x1 152 cell
>> chr = "0.41 8.24 3.57 6.24 9.27";
>> C = textscan(chr,'%f');
>> whos C
Name Size Bytes Class Attributes
C 1x1 152 cell
>> C
C =
1×1 cell array
{5×1 double}
>> [C{:}]
ans =
0.410000000000000
8.240000000000000
3.570000000000000
6.240000000000000
9.270000000000000<br>
slide6. 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 10G15G27G22G30G28G13G18G10G11G08 data desc
-14625784.02147 -11358678.40846 22561784.4224 22561780.0784 data
46.5004 36.2504 data
... 9 more of these (18 lines) ...
16 1 1 0 0 30.0000000 0 11G15G01G27G22G30G28G13G18G10G11G08
... 11 observations (22 lines) ...
15.0000 comment in middle INTERVAL
16 1 1 0 0 45.0000000 0 10G15G01G27G22G30G28G13G18G10G08<br>
slide7. 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/matlab_prog/formatting-strings.html<br>
slide8. 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 'InputFormat' 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.<br>
slide9. >> help datetime
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.<br>
slide10. Somewhere on those screens (hopefully in an example) you will find how to specify the format.
>> datetime.setDefaultFormats('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/matlab_prog/set-display-format-of-date-and-time-arrays.html<br>
slide11. Write 2 programs to read the files
mixedin1.dat and mixedin2.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).<br>
slide12. textscan
Works pretty much like fscanf
https://www.mathworks.com/help/matlab/ref/textscan.html
Reads into cell array.<br>
slide13. Can do simple “edits” on data with formatSpec
Read the same character vector, and truncate each value to one decimal digit.
C = textscan(chr,'%3.1f %*1d');
The specifier %3.1f 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, %*1d, tells textscan to skip the remaining digit.<br>
slide14. fileID = fopen(‘scan1.dat’);
C = textscan(fileID,'%s %s %f32 %d8 %u %f %f %s %f');
fclose(fileID);
whos C
More (poorly documented) format specifiers
(32 is bits – single precision float,
8 is b1ts - 1 byte integer).
C=1×9 cell
Columns 1 through 5 {3x1 cell} {3x1 cell} {3x1 single} {3x1 int8} {3x1 uint32}
Columns 6 through 9 {3x1 double} {3x1 double} {3x1 cell} {3x1 double}
several more examples on this page<br>
slide15. 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.<br>
slide16. type command - displays file in command window
Graphics Handles – from “Getting a handle on Matlab Graphics” by Roger A. Green.
clf, clear, delete<br>
Continue Introduction to Matlab
Character I/O (fscanf, textscan, fgetl)
type command
Graphics Handles
clf, clear, delete
Lab – 8, 09/19/19<br>
slide2. 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(fileID,formatSpec)
Examples in matlab_fscanf_ex.m<br>
slide3. Reading in data
The example files here are 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 formatSpec.<br>
slide4. formatSpec – describes the data.
chr = '0.41 8.24 3.57 6.24 9.27’;
C = textscan(chr,'%f');
Here using variable instead of fileID (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.<br>
slide5. >> chr = '0.41 8.24 3.57 6.24 9.27';
>> C = textscan(chr,'%f');
>> whos C
Name Size Bytes Class Attributes
C 1x1 152 cell
>> chr = "0.41 8.24 3.57 6.24 9.27";
>> C = textscan(chr,'%f');
>> whos C
Name Size Bytes Class Attributes
C 1x1 152 cell
>> C
C =
1×1 cell array
{5×1 double}
>> [C{:}]
ans =
0.410000000000000
8.240000000000000
3.570000000000000
6.240000000000000
9.270000000000000<br>
slide6. 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 10G15G27G22G30G28G13G18G10G11G08 data desc
-14625784.02147 -11358678.40846 22561784.4224 22561780.0784 data
46.5004 36.2504 data
... 9 more of these (18 lines) ...
16 1 1 0 0 30.0000000 0 11G15G01G27G22G30G28G13G18G10G11G08
... 11 observations (22 lines) ...
15.0000 comment in middle INTERVAL
16 1 1 0 0 45.0000000 0 10G15G01G27G22G30G28G13G18G10G08<br>
slide7. 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/matlab_prog/formatting-strings.html<br>
slide8. 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 'InputFormat' 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.<br>
slide9. >> help datetime
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.<br>
slide10. Somewhere on those screens (hopefully in an example) you will find how to specify the format.
>> datetime.setDefaultFormats('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/matlab_prog/set-display-format-of-date-and-time-arrays.html<br>
slide11. Write 2 programs to read the files
mixedin1.dat and mixedin2.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).<br>
slide12. textscan
Works pretty much like fscanf
https://www.mathworks.com/help/matlab/ref/textscan.html
Reads into cell array.<br>
slide13. Can do simple “edits” on data with formatSpec
Read the same character vector, and truncate each value to one decimal digit.
C = textscan(chr,'%3.1f %*1d');
The specifier %3.1f 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, %*1d, tells textscan to skip the remaining digit.<br>
slide14. fileID = fopen(‘scan1.dat’);
C = textscan(fileID,'%s %s %f32 %d8 %u %f %f %s %f');
fclose(fileID);
whos C
More (poorly documented) format specifiers
(32 is bits – single precision float,
8 is b1ts - 1 byte integer).
C=1×9 cell
Columns 1 through 5 {3x1 cell} {3x1 cell} {3x1 single} {3x1 int8} {3x1 uint32}
Columns 6 through 9 {3x1 double} {3x1 double} {3x1 cell} {3x1 double}
several more examples on this page<br>
slide15. 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.<br>
slide16. type command - displays file in command window
Graphics Handles – from “Getting a handle on Matlab Graphics” by Roger A. Green.
clf, clear, delete<br>