Post on 01-Jan-2016
Outline
• 8.1 Concept: Serial Input and Output (I/O)• 8.2 MATLAB Workspace I/O• 8.3 High-level I/O Functions
– 8.3.1 Exploration– 8.3.2 Excel Spreadsheets– 8.3.3 Delimited Text Files—Numerical Data Only
• Intermediate Level via XML: not in the book• 8.4 Low-level file I/O
– 8.4.1 Opening and Closing Files– 8.4.2 Reading Text Files– 8.4.3 Examples of Reading Text Files– 8.4.4 Writing Text Files
Types of Files
1. MATLAB Workspace2. Spreadsheet and text: with delimited
numbers and plain text3. XML: just barely mentioned in the book 4. Image (not in this chapter)5. Sound (not in this chapter)6. Binary: data is not in text form (not in this
chapter)
MATLAB Workspace I/O
• Once you have created something in your workspace you can “save” it.
• You can retrieve what you have saved with “load”.
• There is a default name, matlab.mat, and you can use a name of your own choice if you wish.
Partial MATLAB Workspace
• When saving a workspace, you can specify the subset of the variables you wish to save.
• Save mydata.mat a b c*• Saves variables named a, b and those
beginning with c. The * is a wildcard for matching.
Why Save a Workspace
• Sometimes the calculation of variables takes significant time, and rather than recalculating them, one wishes to save that effort.
Gradation of CommonalityType of File Type of Converter
Well-known, fixed, e.g. Excel Available in MATLAB, xlsread(‘filename’)
Well-known, customizable, e.g., XML Mostly available in MATLAB (!!)
Do-it-yourself, not fixed Do it yourself, with help from MATLAB
High-Level I/O
• For known file types, utilize the work of others:• Plain text textscan, fprintf• Comma-separated csvread, csvwrite• Tab-separated dlmread, dlmwrite• Other delimited dlmread, dlmwrite• Excel xlsread, xlswrite• XML xmlread, xmlwrite• Many others!
Example Excel-1Energizer 15 k 19.93 kdate time datetime voltage
9-Jul-09 16:31 7/9/2009 16:31 40003.68819 3.2510-Jul-09 7:16 7/10/2009 7:16 40004.30278 2.910-Jul-09 9:20 7/10/2009 9:20 40004.38889 2.910-Jul-09 12:10 7/10/2009 12:10 40004.50694 2.910-Jul-09 15:14 7/10/2009 15:14 40004.63472 2.9110-Jul-09 18:52 7/10/2009 18:52 40004.78611 2.9113-Jul-09 9:56 7/13/2009 9:56 40007.41389 2.9313-Jul-09 17:25 7/13/2009 17:25 40007.72569 2.9313-Jul-09 19:56 7/13/2009 19:56 40007.83056 2.9314-Jul-09 10:07 7/14/2009 10:07 40008.42153 2.9414-Jul-09 20:00 7/14/2009 20:00 40008.83333 2.9315-Jul-09 9:26 7/15/2009 9:26 40009.39306 2.9415-Jul-09 20:53 7/15/2009 20:53 40009.87014 2.9416-Jul-09 8:22 7/16/2009 8:22 40010.34861 2.9417-Jul-09 19:19 7/17/2009 19:19 40011.80486 2.9320-Jul-09 13:55 7/20/2009 13:55 40014.57986 2.921-Jul-09 20:16 7/21/2009 20:16 40015.84444 2.923-Jul-09 17:13 7/23/2009 17:13 40017.71736 2.8724-Jul-09 11:45 7/24/2009 11:45 40018.48958 2.86
Example Excel-2[nums txt raw] = xlsread('battEnergizer.xls');
>> nums
nums =
1.0e+004 *
0.0015 NaN 0.0020 NaN NaN NaN NaN NaN 0.0001 NaN 4.0004 0.0003 0.0000 NaN 4.0004 0.0003 0.0000 NaN 4.0004 0.0003 0.0001 NaN 4.0005 0.0003
Example Excel-3>> horizontal = nums(:,3)
horizontal =
1.0e+004 *
0.0020 NaN 4.0004 4.0004 4.0004 4.0005 4.0005 4.0005 4.0007 4.0008 4.0008 4.0008
Example Excel-4>> horizontal = horizontal(3:end,1)
horizontal =
1.0e+004 *
4.0004 4.0004 4.0004 4.0005 4.0005 4.0005 4.0007
Example Excel-5>> vertical=nums(:,4)
vertical =
NaN NaN 3.2500 2.9000 2.9000 2.9000 2.9100 2.9100 2.9300
Example Excel-6>> vertical=vertical(3:end,1)
vertical =
3.2500 2.9000 2.9000 2.9000 2.9100 2.9100 2.9300 2.9300 2.9300
Introduction to XML
• URL: http://www.w3.org/XML/• Blend of well-known and custom– Well-known syntax for schema– Schema describes customization
• Many organizations have created schemas
Example Schema<?xml version="1.0" encoding="utf-8"?> <xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name=“setOfVoltageMeasurements”>
<xs:complexType> <xs:sequence>
<xs:element name=“VoltageMeasurement” maxOccurs="unbounded"> <xs:complexType>
<xs:sequence><xs:element name=“DateTime" ” type=“xs:string” /><xs:element name=“Voltage” type=“xs:string” />
</xs:sequence></xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element></xs:schema>
Example Instance
<setOfVoltageMeasurements><VoltageMeasurement>
<DateTime> 40003.68819</DateTime><Voltage>3.25</Voltage>
</VoltageMeasurement><VoltageMeasurement>
<DateTime> 40004.30278</DateTime><Voltage>2.9</Voltage>
</VoltageMeasurement></setOfVoltageMeasurements>
Example XML-1
>> xDoc = xmlread('exVM.xml')xDoc =[#document: null]allDateTimes =
xDoc.getElementsByTagName('DateTime');allVoltages =
xDoc.getElementsByTagName('Voltage');
Example XML-2 xDoc = xmlread('exVM.xml');allDateTimes =
xDoc.getElementsByTagName('DateTime');allVoltages = xDoc.getElementsByTagName('Voltage');%Note that the index of these lists of elements is zero-
based. for i=0:allDateTimes.getLength-1 thisListItem = allDateTimes.item(i); childNode = thisListItem.getFirstChild; while ~isempty(childNode) %Filter out text, comments, and processing
instructions. if childNode.getNodeType ==
3%childNode.ELEMENT_NODE dateTimeFromXML= char(childNode.getData); dateTimeNum(i+1)=
sscanf(dateTimeFromXML, '%f'); end childNode = childNode.getNextSibling; end end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=0:allVoltages.getLength-1 thisListItem = allVoltages.item(i); childNode = thisListItem.getFirstChild; while ~isempty(childNode) %Filter out text, comments, and processing
instructions. if childNode.getNodeType ==
3%childNode.ELEMENT_NODE voltageFromXML= char(childNode.getData); voltageNum(i+1)=sscanf(voltageFromXML,
'%f'); end childNode = childNode.getNextSibling; end end dateTimeNum voltageNumplot(dateTimeNum, voltageNum)
Concept of Serial I/O
• Reading (or writing) the data through from beginning to end, as opposed to jumping around in the middle
• Breaking up the file into “tokens”, recognizing separators between tokens. The separators are called “delimiters”.
Lower Level—Custom-140003.68819 3.25
40004.30278 2.9
40004.38889 2.9
40004.50694 2.9
40004.63472 2.91
40004.78611 2.91
40007.41389 2.93
40007.72569 2.93
40007.83056 2.93
40008.42153 2.94
Lower Level—Custom-2
fh=fopen('batEnergiz.txt', 'r');ln='';while ischar(ln) ln = fgets(fh); if ischar(ln) fprintf(ln); endendfclose(fh);
Lower Level—Custom-3fh=fopen('batEnergiz.txt', 'r');ln='';linecount = 0;datetime(2,15)='x';voltage(2,15)='v';while ischar(ln) ln = fgetl(fh); linecount = linecount+1; counter = 1 if ischar(ln) while ~isempty(ln) switch(counter) case 1 [thisDateTime,ln] = strtok (ln); datetime(linecount, 1:length(thisDateTime)) = thisDateTime;
case 2 [thisVoltage,ln] = strtok (ln); voltage(linecount,1:length(thisVoltage))
= thisVoltage; end counter = 2 end endenddatetimeVoltagedatetime(2,15)=' ';voltage(2,15)=' ';fclose(fh); for i = 1:linecount-1 dateTimeNum(i)=sscanf(datetime(i,:),'%f'); voltageNum(i)=sscanf(voltage(i,:),'%f');endplot(dateTimeNum, voltageNum)
Lower Level—Custom-4datetime =
40003.68819 40004.30278 x40004.38889 40004.50694 40004.63472 40004.78611 40007.41389 40007.72569 40007.83056 40008.42153 40008.83333 40009.39306
voltage =
3.25 2.9 v2.9 2.9 2.91 2.91 2.93 2.93 2.93 2.94 2.93 2.94
>>