Programming with Matlab Day 5: Debugging, efficiency, advanced types of variables, logical indices, images.

Slides:



Advertisements
Similar presentations
Debugging ACL Scripts.
Advertisements

Previously… We created a simulated temperature reader which alerts if too hot or too cold… Download the solved practice to keep in sync: Thermostat.vi.
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.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
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.
ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
How to Debug VB .NET Code.
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
MATLAB Programming Session
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Matlab tutorial course Lesson 2: Arrays and data types
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
1 Advanced MATLAB Vectors and matrices fprintf Cell arrays Structures Flow of control Vectorization Functions.
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
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.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Chapter 02 (Part III) Introduction to C++ Programming.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: Φ Fax:
Digital Image Processing Lecture4: Fundamentals. Digital Image Representation An image can be defined as a two- dimensional function, f(x,y), where x.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
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.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
Matlab Programming for Engineers
Digital Image Processing Introduction to M-function Programming.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Programming with Matlab Day 2: More about Loops, Vectors and Matrices.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Section 9-1 An Introduction to Matrices Objective: To perform scalar multiplication on a matrix. To solve matrices for variables. To solve problems using.
Improving Matlab Performance CS1114
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
6.S093: Visual Recognition through Machine Learning Competition MATLAB tutorial.
1 Introduction to Matlab. 2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Reading and Writing Image Files
Lecture 5 of Computer Science II
VBA - Excel VBA is Visual Basic for Applications
Repetition Structures Chapter 9
Computer Programming I
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
Scripts & Functions Scripts and functions are contained in .m-files
MATLAB DENC 2533 ECADD LAB 9.
Use of Mathematics using Technology (Maltlab)
Chapter 8 Data Structures: Cell Arrays and Structures
Variables Title slide variables.
Loop Statements & Vectorizing Code
Loop Statements & Vectorizing Code
Programming with Matlab
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
Presentation transcript:

Programming with Matlab Day 5: Debugging, efficiency, advanced types of variables, logical indices, images

Debugging: keyboard (I) n_elems=length(vector); s=0; for c_elems=1:n_elems s=s+vector(c_elems); keyboard end Editor >> vec=[1 2 3]; >> sumador(vec) K>> c_elems c_elems = 1 Command Window Program stops in the point where we inserted the keyboard command. We get control at command window. We now have access to the function’s workspace, in the state it is at that moment

Debugging: keyboard (II) >> sumador(vec) K>> return K>> c_elems c_elems = 2 Command Window return command continues execution of the program, which runs until the next keyboard command (or until the end) n_elems=length(vector); s=0; for c_elems=1:n_elems s=s+vector(c_elems); keyboard end Editor In our example, execution continues one more iteration, until we again encounter the keyboard command >> sumador(vec) K>> dbquit >> Command Window dbquit cancels execution of the program. We go back to our usual command window, with the base workspace.

Debugging: Red dots A red dot will appear if you click on one of these lines. They work exaclty the same way as keyboard

Debugging: F5, F10 They are used when execution is stopped by a keyboard command or by a red dot. F5: Continues execution until the next keyboard/red dot (the same as return command) F10: Executes only the next line of the program.

Debugging: try…catch function c=errores c=1; caca Editor >> errores ??? Undefined function or variable 'caca'. >> Command Window function c=errores2 c=1; try caca catch disp(‘Eres idiota’) keyboard end Editor >> errores Eres idiota K>> Command Window Matlab tries to execute the code in the “try” section. If the code works properly, the “catch” section does not execute. If there is an error in the “try” section, instead of aborting execution, matlab executes the “catch” section.

Debugging/efficiency: Those small orange and red lines Each line indicates that Matlab found something in the code that needs improvement Red lines: Errors. The program will not execute correctly (for example, a parenthesis is missing). Orange lines: Will execute correctly, but it might be better (for example, variables that should be pre-allocated for speed).

Efficiency: tic…toc Timer starts, when tic command is executed. When toc command is executed, we get the time elapsed since the tic. >> tic a=0.1; toc Elapsed time is seconds.

Efficiency: Preallocate variables vec becomes one element longer each iteration. Therefore, Matlab must reserve an extra bit of memory each iteration. This takes A LOT of time. All the memory we are going to need is reserved at the beginning. >> tic for c=1:50000 vec(c)=c; end toc Elapsed time is seconds. >> tic vec=zeros(1,50000); for c=1:50000 vec(c)=c; end toc Elapsed time is seconds. 50 times faster!

Efficiency: Matrices, matrices, matrices! Matlab’s loops are relatively slow But Matlab works very fast with matrices >> x=0:.0001:10; >> exponencial=zeros(1,100001); >> tic for c=1: exponencial(c)=exp(x(c)); end toc Elapsed time is seconds. >> tic exponencial=exp(x); toc Elapsed time is seconds. 20 times faster!

A useful instruction: repmat repmat creates a matrix which is just a vector repeated several times. Exercise tabla5 in two lines: >> mat=repmat(1:5,5,1) mat = >> tabla5=mat.* mat' tabla5 =

Efficiency: Profiler >> profile on >> Affinity_full >> profile viewer The profiler tells you what lines of your program are consuming more time

3D matrices >> matriz(1,3,2) Just like 2D matrices, but adding a third index. And if we come to that… >> matriz(1,3,2,7,14,8) Matlab matrices may have many dimensions

Cells Cells are “matrices of matrices” >> a{1,1}='Holapis'; >> a{1,2}=7; >> a{2,1}=1:4 a = 'Holapis' [7] [1x4 double] [] >> a{2,1}(3) ans = 3 Each component stores a different type of variable. For cells, use braces instead of parenthesis Element 2,1 of the cell (which was the vector with 4 components) Third element of the vector contained in the cell.

Structures Structures are sets of variables of different types >> casita.a=1; >> casita.hola='saludete'; >> casita.matriz=rand(10); >> casita casita = a: 1 hola: 'saludete' matriz: [10x10 double] Name of the structure Dot Name of variable within the structure (called “field”) It is possible to have matrices of structures, so that casita(1) is a complete structure, and casita(2) is another one, with the same fields, but with different values.

Logical indexing (I) >> vector=rand(1,10); >> vector([ ]) >> vector(logical([ ])) Logical variables: >> logical([ ]); Transforms numeric variables in logical ones (0=false, nonzero=true) Logical vector with ‘true’ in the elements that we want

Logical indexing (II) A natural way of creating logical variables: >> vec=[ ]; >> igualesatres = vec==3 igualesatres = True for the elements that are equal to 3

Logical indexing (III) The most basic logical indexing: Not using find >> vec=[ ]; >> dat=[ ]; >> indices=find(vec==3); >> datosbuenos=dat(indices) datosbuenos = >> vec=[ ]; >> dat=[ ]; >>buenos= vec==3; >> datosbuenos=dat(buenos) datosbuenos = >> vec=[ ]; >> dat=[ ]; >> datosbuenos=dat(vec==3) datosbuenos =

Working with images: Load an image >> mat_img=imread('filename.tif'); The image is stored in a matrix (2D matrix if the image is grayscale, 3D matrix if it is a color image) Matlab supports most image formats Each component of the matrix stores the gray level of one pixel of the image (in color images, the 3D matrix has three components in the third dimension. Each of these three matrices stores the component for the r,g,b code of each pixel)

Matlab’s representation of images Pixel number. Rows: First component of the matrix Pixel number. Columns: second component of the matrix