Lecture 7 Debugging Code Data ImportExport 2007 Daniel
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Debugging & importing data n Computer code design (debug errors): – Debugging is a primary task in code design it is the task to correct errors & validate a tool. n Importing/exporting data: – There are many programs that deal with data. – Different programs have their own strengths and weaknesses. – Very often, we use multiple programs and need to share data between them.
Syntax errors n Errors in the MATLAB statement itself, such as spelling or punctuation errors. n Example: n Should be:
Run-time errors Occurs when illegal mathematical operations are attempted during program execution. n Type into an M-file: n n Try running the program and enter a number ≤ 0. Note: The run-time error is %2. 3 d because only the real part of the log is displayed.
Logical errors Occur when the program runs without displaying an error, but produces an unexpected result. n Type into an M-file: n Try running the program, enter 5. n The result will be 10, where is the error? n Note: If you enter 2 instead, you will see that it could be difficult to find logical errors. n
Typical data file types “Binary” n “Machine language” n Fast & Efficient n Not readily readable n Usually proprietary n Usually the “Native” format for a program n Examples –. xls –. doc –. mat ASCII n “Text File” n Can be read in any text editor (e. g. , MATLAB editor or notepad) n Good for sharing n Examples –. txt –. dat –. csv
Import Wizard n The Import Wizard is a feature in MATLAB that determines the type of data file and determines the way to extract and display the information within MATLAB. n It can be used to extract data from ASCII files, Excel spreadsheet files, among others. n Import Wizard is opened by double clicking on a file name in the current directory window of the desktop.
uiimport n Another way to open the Import Wizard is to type uiimport('filename. extension') in the command window. n The single quotes around the name of the file are very important; the wizard will not run if they are omitted.
Importing an Excel data file xlsread('filename. xls') n Before you can use this command you need to ensure that where ever the file is stored is in your path. (File Set path) n Note that if Excel is not installed on the computer MATLAB cannot use xlsread or xlswrite.
Exporting to Excel n An array written in MATLAB can be exported to an Excel document. n The syntax is: xlswrite('filename. xls' , s ) n You need to define s (or any other array of data) before attempting to export data to an Excel readable file.
Hands-on n Write an array of odd numbers from 1 -19. n Save the array to an Excel document. n Clear your workspace and import the array from the Excel readable file you just created.
textread allows MATLAB to read ASCII files. n The file must be formatted into columns, but each column can be a different data type. The construct is: n [a, b, c, d…] = textread(‘filename’, ‘%f %d %d %d…’, n), n a, b, c, d… represent the names of each variable, filename is the name of the file, ‘%f %d %d %d…’ is a string indicating the format of the variables in the text file, and n is number of rows to be read. n If n is not included in the command, the entire file is read.
textread example n If the file ‘sports. dat’ contains: University Soccer 12 University Hockey 15 7 7 2 3 The command you enter to read it is: [sport, wins, losses, ties]. . . = textread(‘sports. dat’, ’. . . %*s, %d, %d’, 2) n %s denotes that column contains strings n %d denotes that column contains integers n If %*s or %*d means that the column will not be read into MATLAB. n
Save & load: *. mat files save filename var 1 var 2 var 3 load filename n n Where filename is the name of the file and var 1, var 2, var 3 are the variables to be saved in a MATLAB readable file. If the variables are not listed after the filename, saves all the variables in the workspace. The save command saves data from the workspace into an *. mat file. The load command loads the variables stored in the filename. mat file previously saved.
Exercises In the command window, define x as 6, t as 14. 5, and r as 22. Save these variables to a file titled “work_data”. Clear the workspace and then load the variables. n Write the table to an Excel file. Load the file into matlab using xlsread. n Team 1 Wins Losse s 11 7 2 6 12 3 10 8
Summary n Errors – Syntax – Run-time – Logical n Data exchange – Import wizard – Importing from and exporting to Microsoft Excel – textread function n Saving and loading files
- Slides: 16