1 / 56

Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research

Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research. Non-Accredited Matlab Tutorial Sessions for beginner to intermediate level users

Download Presentation

Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matlab Beginner Training Session Review:Introduction to Matlab for Graduate Research

  2. Non-Accredited Matlab Tutorial Sessions for beginner to intermediate level users Winter Session Dates:  February 13, 2007 - March 7, 2007Session times: Tuesdays from 8:30am-10:00am, Wednesdays from 8:30pm-10:00amSession Locations: Humphrey Hall - Room 219 Instructors: Robert Marino rmarino@biomed.queensu.ca Course Website: http://www.queensu.ca/neurosci/Matlab Training Sessions.htm

  3. Last Semester Weeks: • Introduction to Matlab and its Interface • Fundamentals (Operators) • Fundamentals (Flow) • Importing Data • Functions and M-Files • Plotting (2D and 3D) • Statistical Tools in Matlab • Analysis and Data Structures

  4. Intermediate Sessions • Intermediate Lectures • Term 1 review • Loading Binary Data • Nonlinear Curve Fitting • Statistical Tools in Matlab II • Creating Graphic User Interfaces (GUIs) • Other possible topics: • Writing ascii text data files • 3D plotting and animating • Debugging Tools • Simulink Toolbox

  5. Why Matlab? Common Uses for Matlab in Research • Data Acquisition • Multi-platform, Multi Format data importing • Analysis Tools (Existing,Custom) • Statistics • Graphing • Modeling

  6. Why Matlab? Multi-platform, Multi Format data importing • Data can be loaded into Matlab from almost any format and platform • Binary data files (eg. REX, PLEXON etc.) • Ascii Text (eg. Eyelink I, II) • Analog/Digital Data files PC 100101010 UNIX Subject 1 143 Subject 2 982 Subject 3 87 …

  7. Why Matlab? Analysis Tools • A Considerable library of analysis tools exist for data analysis • Provides a framework for the design, creation, and implementation of any custom analysis tool imaginable

  8. Why Matlab? Graphing • A Comprehensive array of plotting options available from 2 to 4 dimensions • Full control of formatting, axes, and other visual representational elements

  9. Why Matlab? Modeling • Models of complex dynamic system interactions can be designed to test experimental data

  10. Understanding the Matlab Environment: • Executing Commands • Basic Calculation Operators: • + Addition • - Subtraction • * Multiplication • / Division • ^ Exponentiation

  11. Using Matlab • Solving equations using variables • Matlab is an expression language • Expressions typed by the user are interpreted and evaluated by the Matlab system • Variables are names used to store values • Variable names allow stored values to be retrieved for calculations or permanently saved • Variable = Expression • Or • Expression • **Variable Names are Case Sensitive! >> x * y Ans = 12 >> x / y Ans = 3 >> x ^ y Ans = 36 >> x = 6 x = 6 >> y = 2 y = 2 >> x + y Ans = 8

  12. Using Matlab • Working with Matrices • Matlab works with essentially only one kind of object, a rectangular numerical matrix • A matrix is a collection of numerical values that are organized into a specific configuration of rows and columns. • The number of rows and columns can be any number • Example • 3 rows and 4 columns define a 3 x 4 matrix having 12 elements • A scalar is a single number and is represented by a 1 x 1 matrix in matlab. • A vector is a one dimensional array of numbers and is represented by an n x 1 column vector or a 1 x n row vector of n elements

  13. Exercises Enter the following Matrices in matlab using spaces, commas, and semicolons to separate rows and columns: A = B = D = D = C = E = a 5 x 9 matrix of 1’s

  14. Exercises Change the following elements in each matrix: 76 76 0 A = B = 0 D = 76 0 D = C = 76 E = a 5 x 9 matrix of 1’s 76

  15. Matrix Operations • Indexing Matrices • A = [1 2 4 5 • 6 3 8 2] • The colon operator can can be used to remove entire rows or columns • >> A(:,3) = [ ] • A = [1 2 5 • 6 3 2] • >> A(2,:) = [ ] • A = [1 2 5]

  16. Matrix Operations • Scalar Operations • Scalar (single value) calculations can be can performed on matrices and arrays • Basic Calculation Operators • + Addition • - Subtraction • * Multiplication • / Division • ^ Exponentiation

  17. Matrix Operations • Element by Element Multiplication • Element by element multiplication of matrices is performed with the .* operator • Matrices must have identical dimensions • A = [1 2 B = [1 D = [2 2 E = [2 4 3 6] • 6 3 ] 7 2 2 ] • 3 • 3] • >>A .* D • Ans = [ 2 4 • 12 6]

  18. Matrix Operations • Element by Element Division • Element by element division of matrices is performed with the ./ operator • Matrices must have identical dimensions • A = [1 2 4 5 B = [1 D = [2 2 2 2 E = [2 4 3 6] • 6 3 8 2] 7 2 2 2 2] • 3 • 3] • >>A ./ D • Ans = [ 0.5000 1.0000 2.0000 2.5000 • 3.0000 1.5000 4.0000 1.0000 ]

  19. Matrix Operations • Matrix Exponents • Built in matrix Exponentiation in Matlab is either: • A series of Algebraic dot products • Element by element exponentiation • Examples: • A^2 = A * A (Matrix must be square) • A.^2 = A .* A

  20. Matrix Operations • Shortcut: Transposing Matrices • The transpose of a matrix is the matrix formed by interchanging the rows and columns of a given matrix • A = [1 2 4 5 B = [1 • 6 3 8 2] 7 • 3 • 3] • >> transpose(A) >> B’ • A = [1 6 B = [1 7 3 3] • 2 3 • 4 8 • 5 2]

  21. Relational Operators • Relational operators are used to compare two scaler values or matrices of equal dimensions • Relational Operators • < less than • <= less than or equal to > Greater than >= Greater than or equal to == equal ~= not equal

  22. Relational Operators • Comparison occurs between pairs of corresponding elements • A 1 or 0 is returned for each comparison indicating TRUE or FALSE • Matrix dimensions must be equal! • >> 5 == 5 • Ans 1 • >> 20 >= 15 • Ans 1

  23. Relational Operators A = [1 2 4 5 B = 7 C = [2 2 2 2 6 3 8 2] 2 2 2 2] Try: >>A > B >> A < C

  24. Relational Operators • The Find Function • A = [1 2 4 5 B = 7 C = [2 2 2 2 D = [0 2 0 5 0 2] • 6 3 8 2] 2 2 2 2] • The ‘find’ function can also return the row and column indexes of of matching elements by specifying row and column arguments • >> [x,y] = find(A == 5) • The matching elements will be indexed by (x1,y1), (x2,y2), … • >> A(x,y) = 10 • A = [ 1 2 4 10 • 6 3 8 2 ]

  25. Control and Flow • Control flow capability enables matlab to function beyond the level of a simple desk calculator • With control flow statements, matlab can be used as a complete high-level matrix language • Flow control in matlab is performed with condition statements and loops

  26. Matlab Scripts • Advantages of M-files • Easy editing and saving of work • Undo changes • Readability/Portability - non executable comments can be added using the ‘%’ symbol to make make commands easier to understand • Saving M-files is far more memory efficient than saving a workspace

  27. Condition Statements • It is often necessary to only perform matlab operations when certain conditions are met • Relational and Logical operators are used to define specific conditions • Simple flow control in matlab is performed with the ‘If’, ‘Else’, ‘Elseif’ and ‘Switch’ statements

  28. Condition Statements • If, Else, and Elseif • An if statement evaluates a logical expression and evaluates a group of commands when the logical expression is true • The list of conditional commands are terminated by the end statement • If the logical expression is false, all the conditional commands are skipped • Execution of the script resumes after the end statement • Basic form: • iflogical_expression • commands • end

  29. Condition Statements Example A = 6 B = 0 if A > 3 D = [1 2 6] A = A + 1 elseif A > 2 D = D + 1 A = A + 2 end What is evaluated in the code above?

  30. Condition Statements • Switch • The switch statement can act as many elseif statements • Only the one case statement who’s value satisfies the original expression is evaluated • Basic form: • switchexpression (scalar or string) • casevalue 1 • commands 1 • case value 2 • commands 2 • case value n • commands n • end

  31. Condition Statements • Example • A = 6 B = 0 • switch A • case 4 • D = [ 0 0 0] • A = A - 1 • case 5 • B = 1 • case 6 • D = [1 2 6] • A = A + 1 • end • ** Only case 6 is evaluated

  32. Loops • Loops are an important component of flow control that enables matlab to repeat multiple statements in specific and controllable ways • Simple repetition in matlab is controlled by two types of loops: • For loops • While loops

  33. Loops • For Loops • The for loop executes a statement or group of statements a predetermined number of times • Basic Form: • for index = start:increment:end • statements • end • ** If ‘increment’ is not specified, an increment of 1 is assumed by matlab

  34. Loops • For Loops • Examples: • for i = 1:1:100 • x(i) = 0 • end • Assigns 0 to the first 100 elements of vector x • If x does not exist or has fewer than 100 elements, additional space will be automatically allocated

  35. Loops • For Loops • Loops can be nested in other loops • A = [ ] • for i = 1:m • for j = 1:n • A(i,j) = i + j • end • end • Creates an m by n matrix A whose elements are the sum of their matrix position

  36. Loops • While Loops • The while loop executes a statement or group of statements repeatedly as long as the controlling expression is true • Basic Form: • whileexpression • statements • end

  37. Loops • While Loops • Examples: • A = 6 B = 15 • while A > 0 & B < 10 • A = A + 1 • B = B – 2 • end • Iteratively increase A and decrease B until the two conditions of the while loop are met • ** Be very careful to ensure that your while loop will eventually reach its termination condition to prevent an infinite loop

  38. Loops • Breaking out of loops • The ‘break’ command instantly terminates a for and while loop • When a break is encountered by matlab, execution of the script continues outside and after the loop

  39. Loops • Breaking out of loops • Example: • A = 6 B = 15 • count = 1 • while A > 0 & B < 10 • A = A + 1 • B = B + 2 • count = count + 1 • if count > 100 • break • end • end • Break out of the loop after 100 repetitions if the while condition has not been met

  40. Functions in Matlab • In Matlab, each function is a .m file • It is good protocol to name your .m file the same as your function name, i.e. funcname.m • function outargs=funcname(inargs); Function input output

  41. Importing Data • Basic issue: • How do we get data from other sources into Matlab so that we can play with it? • Other Issues: • Where do we get the data? • What types of data can we import • Easily or Not

  42. Basics • Matlab has a powerful plotting engine that can generate a wide variety of plots.

  43. Generating Data • Matlab does not understand functions, it can only use arrays of numbers. • a=t2 • b=sin(2*pi*t) • c=e-10*t note: matlab command is exp() • d=cos(4*pi*t) • e=2t3-4t2+t • Generate it numerically over specific range • Try and generate a-e over the interval 0:0.01:2 t=0:0.01:10; %make x vector y=t.^2; %now we have the appropriate y % but only over the specified range

  44. Quick Assignment 1 • Plot a as a thick black line • Plot b as a series of red circles. • Label each axis, add a title and a legend

  45. Quick Assignment 1 figure plot(t,a,'k','LineWidth',3); hold on; plot(t,b,'ro') xlabel('Time (ms)'); ylabel('f(t)'); legend('t^2','sin(2*pi*t)'); title('Mini Assignment #1')

  46. Part A: Basics • The Matlab installation contains basic statistical tools. • Including, mean, median, standard deviation, error variance, and correlations • More advanced statistics are available from the statistics toolbox and include parametric and non-parametric comparisons, analysis of variance and curve fitting tools

  47. Mean and Median Mean: Average or mean value of a distribution Median: Middle value of a sorted distribution M = mean(A), M = median(A) M = mean(A,dim), M = median(A,dim) M = mean(A), M = median(A): Returns the mean or median value of vector A. If A is a multidimensional mean/median returns an array of mean values. Example: A = [ 0 2 5 7 20] B = [1 2 3 3 3 6 4 6 8 4 7 7]; mean(A) = 6.8 mean(B) = 3.0000 4.5000 6.0000 (column-wise mean) mean(B,2) = 2.0000 4.0000 6.0000 6.0000 (row-wise mean)

  48. Standard Deviation and Variance • Standard deviation is calculated using the std() function • std(X) : Calcuate the standard deviation of vector x • If x is a matrix, std() will return the standard deviation of each column • Variance (defined as the square of the standard deviation) is calculated using the var() function • var(X) : Calcuate the variance of vector x • If x is a matrix, var() will return the standard deviation of each column

  49. Standard Error of the Mean In Class Exercise 1: • Create a function called se that calculates the standard error of some vector supplied to the function Eg. se(x) should return the standard error of matrix x

  50. Data Correlations • Matlab can calculate statistical correlations using the corrcoef() function • [R,P] = corrcoef(A,B) • Calculates a matrix of R correlation coefficiencts and P significance values (95% confidence intervals) for variables A and B A B R = A AcorA BcorA B AcorB BcorB

More Related