Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.

Similar presentations


Presentation on theme: "Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1."— Presentation transcript:

1 Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1

2 1.1. File Applications Databases Logs – attendance, events, history Journals / Diaries Address books Sensor data Documents Almost every program uses files! 2 Shuttle ECO sensor Automotive O 2 sensor

3 1.2 Data Files Data files can be different types and with different data organization 3

4 1.2 Data Files, cont. Flagler Property Sales 4

5 1.2 Data Files, cont. Grade 5

6 1.3 File I/O There are basically three operation modes on files: – Read from: to grab/load data from a file and pass them to variables (INPUT) – Write to: to store/save data to a file from the file beginning (OUTPUT) – Append to: to add/save data to the end of a file (OUTPUT) In general, 3 steps are always involved: – MATLAB opens the file; – MATLAB reads from, writes to, or appends to the file; – MATLAB close the file; 6

7 2.1 High-level File I/O A file I/O function is considered HIGH-LEVEL if in ONE SINGLE COMMAND, it 1.Opens the file 2.Reads from the file, writes to the file, or appends to the file 3.Closes the file We’re not doing low-level files this semester. Feel free to examine old lecture notes… 7

8 2.2 Can High-level be used? To decide if using a high level function is possible, evaluate the following: 1.What type of file is it? (Excel, text, jpg..) 2.Recognize the data types (all numerical? all strings? Or combination of both) 3.Find the organization overall (data in rows, or data in columns, data placed all over the place…) 4.Recognize the delimiters: What makes it obvious it is a new column? What makes it obvious it is a new row? space, tabs, comma, new lines, dash, colon, /, any specific symbol! 8

9 2.2 Can High-level Be Used? 9

10 3.1 ASCII Delimited Files "Neatly Organized" 1.Rows and Columns are "identifiable" 2.Always the same patterns per line 3.There is no mixes of delimiters (commas, spaces, tabs, dash.. ) 10 A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streamscharactersplain text

11 Questions Can these files be read using dlmread() ? 11

12 The data files (.txt,.xls,.xlsx,.m) should all be in the same directory for function calls to work! 12

13 3.2 dlmread() Syntax M = dlmread(filename); Reads numeric data from the ASCII delimited file filename, and returns the data in output matrix M. The input filename is a string enclosed in single quotes. The delimiter is inferred from the formatting of the file. M = dlmread(filename, delimiter); Reads numeric data from the ASCII delimited file filename using the delimiter delimiter such as '-', ':' and etc. (Use '\t' to specify a tab.) >>doc dlmread open further possible syntaxes 13

14 3.3 Specific Delimiters, cont. Delimiter: other than the defaults 14 Specify the delimiter as the 2 nd argument.

15 3.3 Using Delimiters, cont. BAD… Mix of delimiters. 15 Two delimiters (spaces and colons) in the file. MATLAB is lost. >> "low-level" functions are necessary for this

16 Example1: airfoils The NACA airfoils are airfoils shapes for aircraft wings. Load the data (x and y coordinates) for this model of NACA airfoil and plot it. 16 www.angelfire.com

17 Example1: airfoils clc clear %load the numerical data file %slice the array to extract x and y coordinates % plot the airfoil and format the plot plot(x,y) title('Naca2410') %add plot title axis equal %make axis equal in scale grid on %put the grid on 17 array = dlmread('naca2410.dat'); x = array(:,1); y = array(:,2); Remember those quotes and the extension.

18 without files… clc clear %prompt how many sets of x-y coordinates sets = input('How many sets (>5)? '); while sets<=5 || mod(sets,1)~=0 %decimals sets = input('ERROR: How many sets (>5)? '); end %prompt user for x/y coordinates for k = 1:sets %ask for x fprintf('x%d: ',k); x(k) = input(''); %ask for y fprintf('y%d: ',k); y(k) = input(''); fprintf('\n'); end % plot the airfoil and format the plot plot(x,y) title('Naca2410') %add plot title axis equal %make axis equal in scale grid on %put the grid on 18

19 2. xlsread() 19

20 2. xlsread(), Syntax Syntax [num,txt,raw] = xlsread(filename) reads data from the first worksheet; [num,txt,raw] = xlsread(filename, sheet) reads the specified worksheet. Numeric data in the spreadsheet are returned to array num. Optional: Textual information in the spreadsheet is returned to cell array txt, and the unprocessed data (numbers and text) to cell array raw. filename is a string. If hardcoded, it must be in single quotes. 20

21 Example: xlsread() Load the grade data from a spreadsheet and find the name of the student who has the highest grade. 21 >>[age_grade, text, raw] = xlsread('grades.xlsx')

22 22 age_grade = 19 78 22 83 98 99 21 56 23 89 19 51 myText= 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy' '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' '' >> [age_grade, myText, raw] = xlsread('grades.xlsx') 6X2 double 7X3 cell raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51] Example: xlsread()

23 23 myText = 19 78 22 83 98 99 21 56 23 89 19 51 age_grade = 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy' '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' '' raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51] Example: xlsread() variable names don't matter to MATLAB. It respects its order. >> [myText, age_grade, raw] = xlsread('grades.xlsx')

24 Example: xlsread() Now find student who has the highest grade. 24 age_grade = 19 78 22 83 98 99 21 56 23 89 19 51 text= 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy' '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' '' raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51]

25 Example: xlsread() Find the student who has the highest grade %load data from file [age_grade, text] = xlsread('grades.xlsx' ); %find the position of the maximum grade [trash row]= max(age_grade(:,2) ); %find whose name is associated with that position name = text{row+1,1} ; %+1 due to headers fprintf('%s got the highest grade\n', name) NOTE: raw was not necessary for this. We simply did not collect it… 25

26 xlsread(), cont. OMIT collecting the 2nd and 3rd return variables if only numerical values are of interest! age_grade = xlsread('grades.xlsx'); If a project needs all the data together, collect the 1st and 2nd return values into a dummy variable. [trash trash data] = xlsread('grades.xlsx'); or since MATLAB R2009b, use tilde (~): [~, ~, data] = xlsread('grades.xlsx'); If there happens to be 'holes' in the spreadsheet, MATLAB fills it with a NaN value (not a number). The function isnan() can help determine where those 'holes' are. 26

27 Testing habits Though the file may contain 1,000,000 lines, and ONE command does the job, it is best to test the analysis with a smaller file: – create a copy of the original file, where only 5-10 lines are present. 27


Download ppt "Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1."

Similar presentations


Ads by Google