Matlab Training Session 1 Introduction to Matlab for
Matlab Training Session 1: Introduction to Matlab for Beginner Course Website: http: //www. queensu. ca/neurosci/matlab. php
Course Outline § Each weekly session will independently cover a new and progressively more advanced Matlab topic 1. Introduction to Matlab and its Interface 2. Fundamentals (Operators) 3. Fundamentals (Flow) 4. Importing Data 5. Functions and M-Files 6. Plotting (2 D and 3 D) 7. Statistical Tools in Matlab 8. Analysis and Data Structures
Week 1: Lecture Outline An Introduction to Matlab and its Interface
A. Why Matlab? § Some Common Uses for Matlab in Research B. Understanding the Matlab Environment: § § Navigating the Matlab Desktop Commonly used Toolbox Components Executing Commands Help and Documentation § § Matrices, Scalars and Arrays Useful Commands Searching and Indexing Saving and Reloading Work C. Using Matlab: D. Exercises
Why Matlab? ► Matlab : Matrix Labratory ► Created in late 1970’s ► Intended for used in courses in matrix theory, linear algebra and numerical analysis ► Currently has grown into an interactive system and high level programming language for general scientific and technical computation
Why Matlab? Common Uses for Matlab in Research ► Data Acquisition ► Multi-platform, Multi Format data importing ► Analysis Tools (Existing, Custom) ► Statistics ► Graphing ► Modeling
Why Matlab? Data Acquisition ► A framework for bringing live, measured data into MATLAB using PC-compatible, plug-in data acquisition hardware
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 …
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
Why Matlab? Statistical Analysis ► A considerable variety of statistical tests available including: § TTEST § Mann-Whitney Test § Rank Sum Test § ANOVAs § Linear Regressions § Curve Fitting
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
Why Matlab? Modeling ► Models of complex dynamic system interactions can be designed to test experimental data
Understanding the Matlab Environment: Navigating the Matlab Desktop Commonly Used Toolboxes Executing Commands Basic Calculation Operators: + Addition - Subtraction * Multiplication / Division ^ Exponentiation
Using Matlab Solving equations using variables 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 >> x * y >> x = 6 Ans = 12 Variable = Expression x=6 >> x / y Or >> y = 2 Ans = 3 Expression y=2 >> x ^ y >> x + y Ans = 36 **Variable Names are Case Sensitive! Ans = 8
Using Matlab Working with Matrices • Matlab works with essentially 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
Using Matlab Working with Matrices • Matlab works with essentially 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
Using Matlab Working with Matrices c = 5. 66 or c = [5. 66] x = [ 3. 5, 33. 22, 24. 5 ] x 1 = [ 2 5 3 -1] A=[1 2 4 2 -2 2 0 3 5 5 4 9] c is a scalar or a 1 x 1 matrix x is a row vector or a 1 x 3 matrix x 1 is column vector or a 4 x 1 matrix A is a 4 x 3 matrix
Using Matlab Working with Matrices • Spaces, commas, and semicolons are used to separate elements of a matrix • Spaces or commas separate elements of a row [1 2 3 4] or [1, 2, 3, 4] • Semicolons separate columns [1, 2, 3, 4; 5, 6, 7, 8; 9, 8, 7, 6] = [1 2 3 4 5678 9 8 7 6]
Using Matlab Indexing Matrices • A m x n matrix is defined by the number of m rows and number of n columns • An individual element of a matrix can be specified with the notation A(i, j) or Ai, j for the generalized element, or by A(4, 1)=5 for a specific element. Example: >> A = [1 2 4 5; 6 3 8 2] A is a 2 x 4 matrix >> A(1, 2) Ans 2 • The colon operator can be used to index a range of elements >> A(1, 1: 3) Ans 1 2 4
Using Matlab Indexing Matrices • Specific elements of any matrix can be overwritten using the matrix index Example: A = [1 2 4 5 6 3 8 2] >> A(2, 1) = 9 Ans A = [1 2 4 5 9 3 8 2]
Using Matlab Matrix Shortcuts • The ones and zeros functions can be used to create any m x n matrices composed entirely of ones or zeros Example a = ones(2, 3) a = [1 1 1 1] b = zeros(1, 5) b = [0 0 0]
Using Matlab Data Types and Formats • The semicolon operator determines whether the result of an expression is displayed • who • whos lists all of the variables in your matlab workspace list the variables and describes their matrix size
Using Matlab Saving your Work To save data to a *. mat file: Typing ‘save filename’ at the >> prompt and the file ‘filename. mat’ will be saved to the working directory Select Save from the file pull down menu To reload a *. mat file 1. Type ‘load filename’ at the >> prompt to load ‘filename. mat’ (ensure the filename is located in the current working directory) 2. Select Open from the file pull down menu and manually find the datafile
Getting Help and Documentation Digital 1. Updated online help from the Matlab Mathworks website: www. mathworks. com/access/helpdesk/help/techdoc/matlab. html 3. Matlab command prompt function lookup 4. Built in Demo’s 5. Websites Hard Copy 3. Books, Guides, Reference The Student Edition of Matlab pub. Mathworks Inc.
Exercises Enter the following Matrices in matlab using spaces, commas, and semicolons to separate rows and columns: A= B= D= C= D= E = a 5 x 9 matrix of 1’s
Exercises Use the who and whos functions to confirm all of the variables and matrices in the work space are present and correct A= B= E= C= D= E = a 5 x 9 matrix of 1’s
Exercises Change the following elements in each matrix: 76 A= 76 B= 0 0 D= C= 76 D= 76 0 E = a 5 x 9 matrix of 1’s 76
- Slides: 27