Download presentation
Presentation is loading. Please wait.
Published byRachel Franklin Modified over 9 years ago
1
Lecture 13 File Input/Output From Chapter 8
2
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
3
Types of Files 1.MATLAB Workspace 2.Spreadsheet and text: with delimited numbers and plain text 3.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)
4
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.
5
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.
6
Why Save a Workspace Sometimes the calculation of variables takes significant time, and rather than recalculating them, one wishes to save that effort.
7
Gradation of Commonality Type of FileType of Converter Well-known, fixed, e.g. ExcelAvailable in MATLAB, xlsread(‘filename’) Well-known, customizable, e.g., XMLMostly available in MATLAB (!!) Do-it-yourself, not fixedDo it yourself, with help from MATLAB
8
High-Level I/O For known file types, utilize the work of others: Plain texttextscan, fprintf Comma-separatedcsvread, csvwrite Tab-separateddlmread, dlmwrite Other delimiteddlmread, dlmwrite Excelxlsread, xlswrite XMLxmlread, xmlwrite Many others!
9
Example Excel-1
10
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.0001 NaN 4.0005 0.0003
11
Example Excel-3 >> horizontal = nums(:,3) horizontal = 1.0e+004 * 0.0020 NaN 4.0004 4.0005 4.0007 4.0008
12
Example Excel-4 >> horizontal = horizontal(3:end,1) horizontal = 1.0e+004 * 4.0004 4.0005 4.0007
13
Example Excel-5 >> vertical=nums(:,4) vertical = NaN 3.2500 2.9000 2.9100 2.9300
14
Example Excel-6 >> vertical=vertical(3:end,1) vertical = 3.2500 2.9000 2.9100 2.9300
15
Example Excel-7 plot(horizontal, vertical)
16
Introduction to XML URL: http://www.w3.org/XML/http://www.w3.org/XML/ Blend of well-known and custom – Well-known syntax for schema – Schema describes customization Many organizations have created schemas
17
Example Schema
18
Example Instance 40003.68819 3.25 40004.30278 2.9
19
Example XML-1 >> xDoc = xmlread('exVM.xml') xDoc = [#document: null] allDateTimes = xDoc.getElementsByTagName('DateTime'); allVoltages = xDoc.getElementsByTagName('Voltage');
20
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 %%%%%%%%%%%%%%%% 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 dateTimeNum voltageNum plot(dateTimeNum, voltageNum)
21
Example XML-3 dateTimeNum = 1.0e+004 * 4.0004 voltageNum = 3.2500 2.9000
22
Example XML-4
23
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”.
24
Lower Level—Custom-1 40003.688193.25 40004.302782.9 40004.388892.9 40004.506942.9 40004.634722.91 40004.786112.91 40007.413892.93 40007.725692.93 40007.830562.93 40008.421532.94
25
Lower Level—Custom-2 fh=fopen('batEnergiz.txt', 'r'); ln=''; while ischar(ln) ln = fgets(fh); if ischar(ln) fprintf(ln); end fclose(fh);
26
Lower Level—Custom-3 fh=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 datetime Voltage datetime(2,15)=' '; voltage(2,15)=' '; fclose(fh); for i = 1:linecount-1 dateTimeNum(i)=sscanf(datetime(i,:),'%f'); voltageNum(i)=sscanf(voltage(i,:),'%f'); end plot(dateTimeNum, voltageNum)
27
Lower Level—Custom-4 datetime = 40003.68819 40004.30278 x 40004.38889 40004.50694 40004.63472 40004.78611 40007.41389 40007.72569 40007.83056 40008.42153 40008.83333 40009.39306 voltage = 3.25 2.9 v 2.9 2.91 2.93 2.94 2.93 2.94 >>
28
Lower Level—Custom-5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.