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

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Introduction to MATLAB
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Tutorial 8: Developing an Excel Application
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
Introduction to MATLAB
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Digital Image Processing Lecture3: Introduction to MATLAB.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
A First Program Using C#
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Compiled Matlab on Condor: a recipe 30 th October 2007 Clare Giacomantonio.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Just as there are many human languages, there are many computer programming languages that can be used to develop software. Some are named after people,
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Data Structure & File Systems Hun Myoung Park, Ph.D., Public Management and Policy Analysis Program Graduate School of International Relations International.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
ISU Basic SAS commands Laboratory No. 1 Computer Techniques for Biological Research Animal Science 500 Ken Stalder, Professor Department of Animal Science.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
GENERATION OF RANDOM NUMBERS
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 3: Array Operations Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Debugging M-Files Steve Gu Feb 08, Outline What’s Debugging? Types of Errors Finding Errors Debugging Example Using Debugging Features.
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
Software Development Languages and Environments. Computer Languages Just as there are many human languages, there are many computer programming languages.
Excel Tutorial 8 Developing an Excel Application
ME 142 Engineering Computation I
IMPORTING AND EXPORTING DATA
Other Kinds of Arrays Chapter 11
Jeff Henrikson Lecture 11 Data Import & Export Jeff Henrikson 1.
Lecture 1: Introduction
Advanced Data Import & Export Jeff Henrikson
Digital Image Processing
funCTIONs and Data Import/Export
Working With Data.
Presentation transcript:

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