Sie sind auf Seite 1von 16

Lecture 7

Debugging Code
&
Data Import/Export

2016 Daniel Valentine. All rights reserved. Published by


Elsevier.
Debugging & importing data
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:
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.

Example:

Should be:
Run-time errors
Occurs when illegal mathematical operations are
attempted during program execution.
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.
Logical errors
Occur when the program runs without displaying
an error, but produces an unexpected result.
Type into an M-file:

Try running the program, enter 5.


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.
Typical data file types
Binary ASCII
Machine language Text File
Fast & Efficient Can be read in any
Not readily readable text editor (e.g.,
Usually proprietary MATLAB editor)
Good for sharing
Usually the native
format for a program Examples
Examples .txt
.xls .dat
.doc .csv
.mat
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.
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.
uiimport
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.
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)
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.
The syntax is:

xlswrite('filename.xls' , s )
You need to defines (or any other array of
data) before attempting to export data to
an Excel readable file.
Exercise
Write an array of odd numbers from 1-19.

Save the array to an Excel document.

Clear your workspace and import the array


from the Excel readable file you just
created.
textread
textread allows MATLAB to read ASCII text files.
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.
If n is not included in the command, the entire file
is read.
textread example
If the file sports.dat contains:
University Soccer 12 7 2
University Hockey 15 7 3
The command you enter to read it is:
[sport, wins, losses, ties] ...
= textread(sports.dat, ...
%*s,%s,%d,%d,%d,2)
%s denotes that that column contains strings
%d denotes that that column contains integers
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.
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 load command loads the variables stored in
the filename.mat file previously saved.
Exercises
In the command window, Team Wins Losse
define x as 6, t as 14.5, s
and r as 22. Save these
variables to a file titled 1 11 7
work_data. Clear the
workspace and then load 2 6 12
the variables.
Write the table to an
Excel file. Load the file 3 10 8
into matlab using xlsread.
Summary
Errors
Syntax
Run-time
Logical
Data exchange
Import wizard
Importing from and exporting to Microsoft Excel
textread function
Saving and loading files

Das könnte Ihnen auch gefallen