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