Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved....

16
Lecture 7 Lecture 7 Debugging Code Debugging Code & & Data Import/Export Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Transcript of Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved....

Page 1: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Lecture 7Lecture 7Debugging CodeDebugging Code

& & Data Import/ExportData Import/Export

© 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Page 2: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Debugging & importing dataDebugging & importing data

Computer code design (debug errors):Computer code design (debug errors):– Debugging is a primary task in code design it Debugging is a primary task in code design it

is the task to correct errors & validate a tool.is the task to correct errors & validate a tool. Importing/exporting data:Importing/exporting data:

– There are many programs that deal with data.There are many programs that deal with data.– Different programs have their own strengths Different programs have their own strengths

and weaknesses.and weaknesses.– Very often, we use multiple programs and Very often, we use multiple programs and

need to share data between them.need to share data between them.

Page 3: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

sin(pisin(pi))

sni(pisni(pi))

Syntax errorsSyntax errors

Errors in the MATLAB statement itself, such Errors in the MATLAB statement itself, such as spelling or punctuation errors.as spelling or punctuation errors.

Example:Example:

Should be:Should be:

Page 4: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

x=x=input(input(‘‘EnterEnter a numbera number’’););y=y=log(xlog(x););fprintf(fprintf(‘‘TheThe log of your number is %2.3d.log of your number is %2.3d.\\nn’’,y);,y);

Run-time errorsRun-time errors

Occurs when illegal mathematical operations are Occurs when illegal mathematical operations are attempted during program execution.attempted during program execution.

Type into an M-file:Type into an M-file:

Try running the program and enter a number ≤ 0. Try running the program and enter a number ≤ 0. Note: The run-time error is %2.3d because only Note: The run-time error is %2.3d because only the real part of the log is displayed.the real part of the log is displayed.

Page 5: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

side=side=input(input(‘‘WhatWhat is the side of your square? is the side of your square? ’’); ); area = side + side;area = side + side;fprintf(fprintf(‘‘TheThe area is %2.3d.area is %2.3d.\\nn’’, area);, area);

Logical errorsLogical errors

Occur when the program runs without displaying Occur when the program runs without displaying an error, but produces an unexpected result.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 Note: If you enter 2 instead, you will see that it could

be difficult to find logical errors.be difficult to find logical errors.

Page 6: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Typical data file typesTypical data file types

““Binary”Binary” ASCIIASCII “ “Machine language”Machine language” Fast & EfficientFast & Efficient Not readily readableNot readily readable Usually proprietaryUsually proprietary Usually the “Native” Usually the “Native” format for a programformat for a program ExamplesExamples

– .xls.xls–.doc.doc–.mat.mat

“ “Text File”Text File” Can be read in any Can be read in any text editor (e.g., text editor (e.g., MATLAB editor or MATLAB editor or notepad)notepad) Good for sharingGood for sharing ExamplesExamples

–.txt.txt–.dat.dat–.csv.csv

Page 7: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Import WizardImport Wizard

The The Import WizardImport Wizard is a feature in MATLAB is a feature in MATLAB that determines the type of data file and that determines the type of data file and determines the way to extract and display determines the way to extract and display the information within MATLAB. the information within MATLAB.

It can be used to extract data from ASCII It can be used to extract data from ASCII files, Excel spreadsheet files, among files, Excel spreadsheet files, among others. others.

Import Wizard is opened by double Import Wizard is opened by double clicking on a file name in the current clicking on a file name in the current directory window of the desktop.directory window of the desktop.

Page 8: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

uiimportuiimport

Another way to open the Import Wizard is Another way to open the Import Wizard is to typeto type uiimport(uiimport(''filename.extensionfilename.extension'')) in the command window.in the command window.

The single quotes around the name of the The single quotes around the name of the file are file are very importantvery important; the wizard will not ; the wizard will not run if they are omitted.run if they are omitted.

Page 9: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Importing an Excel data fileImporting an Excel data file

xlsread('filename.xls')xlsread('filename.xls') Before you can use this command you Before you can use this command you

need to ensure that where ever the file is need to ensure that where ever the file is stored is in your path. (File stored is in your path. (File Set path) Set path)

Note that if Excel is not installed on the Note that if Excel is not installed on the computer MATLAB cannot use xlsread or computer MATLAB cannot use xlsread or xlswrite.xlswrite.

Page 10: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Exporting to ExcelExporting to Excel

An array written in MATLAB can be An array written in MATLAB can be exported to an Excel document.exported to an Excel document.

The syntax is:The syntax is:

xlswrite('filename.xls' , s )xlswrite('filename.xls' , s )

You need to define You need to define ss (or any other array of (or any other array of data) before attempting to export data to data) before attempting to export data to an Excel readable file.an Excel readable file.

Page 11: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Hands-onHands-on

Write an array of odd numbers from 1-19.Write an array of odd numbers from 1-19.

Save the array to an Excel document.Save the array to an Excel document.

Clear your workspace and import the array Clear your workspace and import the array from the Excel readable file you just from the Excel readable file you just created.created.

Page 12: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

textreadtextread textreadtextread allows MATLAB to read ASCII files.allows MATLAB to read ASCII files. The file must be formatted into columns, but each The file must be formatted into columns, but each

column can be a different data type. The construct is:column can be a different data type. The construct is:

[a,b,c,d…] = textread(‘filename’, ‘%f %d [a,b,c,d…] = textread(‘filename’, ‘%f %d %d %d…’, n)%d %d…’, n),,

a,b,c,d…a,b,c,d… represent the names of each variable, represent the names of each variable, filename is the name of the file,filename is the name of the file, ‘%f %d %d %d…’‘%f %d %d %d…’ is a is a string indicating the format of the variables in the string indicating the format of the variables in the text file, and text file, and nn is number of rows to be read. is number of rows to be read.

If If nn is not included in the command, the entire file is not included in the command, the entire file is read.is read.

Page 13: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

textreadtextread example example If the file ‘sports.dat’ contains:If the file ‘sports.dat’ contains:

University Soccer 12 7 2University Soccer 12 7 2University Hockey 15 7 3University Hockey 15 7 3

The command you enter to read it is:The command you enter to read it is:[sport, wins, losses, ties][sport, wins, losses, ties]......

= textread(‘sports.dat’,’= textread(‘sports.dat’,’...... %*s,%s,%d,%d,%d’,2)%*s,%s,%d,%d,%d’,2) %s%s denotes that that column contains strings denotes that that column contains strings %d%d denotes that that column contains integers denotes that that column contains integers IfIf %*s %*s or or %*d%*d means that the column will not means that the column will not

be read into MATLAB.be read into MATLAB.

Page 14: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Save & load: *.mat filesSave & load: *.mat files

save filename var1 var2 var3save filename var1 var2 var3load filenameload filename

Where filename is the name of the file and var1, Where filename is the name of the file and var1, var2, var3 are the variables to be saved in a var2, var3 are the variables to be saved in a MATLAB readable file.MATLAB readable file.

If the variables are not listed after the filename, If the variables are not listed after the filename, save saves all the variables in the workspace.save saves all the variables in the workspace.

The save command saves data from the The save command saves data from the workspace into an workspace into an *.mat*.mat file. file.

The load command loads the variables stored inThe load command loads the variables stored in the the filename.matfilename.mat file previously saved. file previously saved.

Page 15: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

ExercisesExercises

In the command window, In the command window, define x as 6, t as 14.5, define x as 6, t as 14.5, and r as 22. Save these and r as 22. Save these variables to a file titled variables to a file titled “work_data”. Clear the “work_data”. Clear the workspace and then load workspace and then load the variables.the variables.

Write the table to an Write the table to an Excel file. Load the file Excel file. Load the file into matlab using xlsread. into matlab using xlsread.

TeamTeam WinsWins LosseLossess

11 1111 77

22 66 1212

33 1010 88

Page 16: Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

SummarySummary ErrorsErrors

– SyntaxSyntax– Run-timeRun-time– LogicalLogical

Data exchangeData exchange– Import wizardImport wizard– Importing from and exporting to Microsoft ExcelImporting from and exporting to Microsoft Excel– textread functiontextread function

Saving and loading filesSaving and loading files