Introduction to PsychToolbox in MATLAB

Introduction to PsychToolbox in MATLAB

What is Matlab? MATLAB is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as C/C++ or Java. You can use MATLAB for a range of applications, including signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology. More than a million engineers and scientists in industry and academia use MATLAB, the language of technical computing. The basic data element is the array, usually referred to as a vector or matrix

learn how to program because Increase your freedom Increase your scientific value Enjoyment Exercise your logical mind PROGRAMMING == PROBLEM SOLVING the concept of an ALGORITHM The process of programming Programming is not a linear process Lots of trial and error Problem solving, detective work, deductive reasoning Debugging may take longer than initial writing.

Enjoy it! Coding philosophy There are many different ways to make things work. The simplest way is usually the most desirable, but: Top priority is that your script does what you want it to do. Good coding practices are important Assume you will remember nothing next time you look at your code Assume someone else will be using your code Assume your script will at some point move to another computer Navigating the Matlab

Desktop Command Window Command History All the contents of the Current Folder where you can save your work

Workspace Browser source: The MathWorks Inc Contains a record of all commands entered in the Command Window Current Folder Browser Where you type the

commands into Matlab Where your data and variables are stored in current Matlab session Desktop Layout source: The MathWorks Inc Desktop Layout source: The MathWorks Inc The file browser Moving around through the folder hierarchy

Command line tools for navigation pwd where are we? cd change directory ls list directory contents . current directory .. parent directory The workspace and variable editor settings variables: x=3 clearing variables: clear x

clear all saving & loading variables: save data load data.mat Getting help help function doc function lookfor function >> help sin sin Sine of argument in radians. sin(X) is the sine of the elements of X. See also asin, sind. Reference page in Help browser

doc sin Scripts Anything you type into the workspace can also be run from a script .m files are just saved lists of matlab commands functions The Editor Comments Syntax highlighting Code folding Variable types Numbers

double: floating point number like 3.24 integer: no decimal places 345 Basic calculations in Matlab Matlab is a powerful graphical calculator Functions? Square root Exponential Absolute value source: The MathWorks Inc

Basic calculations in Matlab Basic calculations in Matlab - each result is stored in its own variable in the workspace and can be used later in the same session - at any time you can clear (some or all) your variables

- if no specific variable name is assigned to the result, the default is ans source: The MathWorks Inc Basic calculations in Matlab Variables are data containers All variables are arrays Scalar (1x1) Vectors (row or column) Matrices (m x n) Default data type is double double = 8 bytes

16 digits of precision source: The MathWorks Inc Basic calculations in Matlab Creating Variables salary = 900 Variable name Assign operator value

Variables are case sensitive ie. thisvar and Thisvar and THISVAR are all different whos = gives a list of the variables in the workspace Matlab uses the first 31 characters of a variable name only source: The MathWorks Inc Basic calculations in Matlab: Variables Once variables or graphics are created, they remain stored in memory if not explicitly cleared or overwritten. This is a common source of errors, since old variables may be mixed with new ones. Clear variables: clear all % deletes all variables, compiled functions, etc

clc % clears the Command Window close all % close all figures Source: HKA & AB slides Basic calculations in Matlab: Variables Syntax errors source: The MathWorks Inc Vectors and matrices Vectors are like lists a = [1,2,3,4,5] Matrices are like lists of lists

a = [ 1,3,5,7; 2,4,6,8 ] Matrices can have many dimensions Creating Vectors Vectors are one dimensional arrays (either column or row vectors) m = 1 5 9 2 6

10 Column vector source: The MathWorks Inc 3 7 11 4 8 12 Row vector

Creating vectors >> a = [1 2 3 4 5] a = 1 2 3 4 5 >> a = [1,2,3,4,5] a = 1 2 3

4 5 >> a = [1:5] a = 1 2 4 5 3

>> x = [ 1 2 3 ]; y = [ 4 5 6 ]; >> z = [ x y ] z = 1 2 3 4 5 6 >> z = [x ; y]; % this is a matrix Creating Vectors Also: >> x = x transpose Example: >> x=[1:3:13]

>> x = x x = a : dx : b first number last number Interval (default is 1) source: The MathWorks Inc For output not to appear on screen but be stored in the workspace use semicolon: >> x=[1:3:13]; Can be used to give multiple commands

>> a = 1; b = 2; c = 3; Creating vectors Data can be created in a linear sequence using built in Matlab functions linspace(x1, x2, N) This function generates N points linearly between X1 and X2. For example: >> a = linspace(1,19,10) a = 1 3 5 7 9 11 13 15 17 19 If N is not defined it defaults to 100 Creating Matrices Matrices are two dimensional arrays A couple common ways to create matrices:

Use square brackets when creating arrays source: The MathWorks Inc Creating matrices >> a = [1 2 3; 4 5 6] a = 1 2 3 4 5 6 >> a = [1 2 3; 4 5 6; 7 8 9] a = 1

2 3 4 5 6 7 8 9 Creating matrices >> ones(3) ans = 1 1 1 1

1 1 1 1 1 >> ones(2,3) ans = 1 1 1 1 1 1

>> zeros(3,4) ans = 0 0 0 0 0 0 0 0 0 (rows,columns)

0 0 0 Creating matrices >> rand(3) ans = 0.8147 0.9058 0.1270 >> nan(4) ans = NaN NaN NaN NaN

NaN NaN NaN NaN 0.9134 0.6324 0.0975 NaN NaN NaN NaN 0.2785 0.5469

0.9575 NaN NaN NaN NaN NaN = Not a Number Creating Matrices Other ways to enter matrices:

Loaded from external data files (check save & load) Generated using built-in functions Created with defined functions in M-files Source: HKA & AB slides Describing matrices size() will tell you the dimensions of a matrix length() will tell you the length of a vector

Exercises Create a row vector from 0 to 27 with intervals of 3 Create a column vector with the same contents What are the remainders after dividing this vector by 2? Create the first 11 perfect squares Create a column vector with the elements from 100 to 0 Find the cube root of 8 (order of operations: MDAS) e to the power of 3 (use help to find the right function) 3cos(), sin([0, /4, /2, 3/4, ]) logarithm of 1000 Create a row vector with the odd numbers between 0 and 50

source: HKA & AB slides Create a matrix m that contains two rows of data. The top row are the numbers 4, 5 and 6 and the bottom row 9, 6 and 3. Create a matrix n that contains two columns of data. The 1st column are the numbers 1, 3, 5 and 7 and the 2nd column are 2, 4, 6 and 8. What is: (1) m.^2 (2) n.^2 (3) n*m (4) m'*n' A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] sum (A) sum the transpose of A

sum the elements on the main diagonal (address the elements directly or use the diag() command) Remind you of anything ? help magic Create a matrix A that increases in steps of 1 from 3 up to 107 Create a matrix B that decreases in steps of 0.5 from 333 down to -10 Create a 2d matrix called C of size 100 rows by 100 columns containing only the number 1 Create a 2d matrix called C of size 100 rows by 200 columns containing only the number 2 Try defining: D = [ 1, 2, 3, 4; 5 6 7 ] What did I do wrong? If I define E = [ 10:1 ] what would you expect E to be? Try it. Can you understand what has happened?

Investigate the Matlab GUI and make sure you know where the different parts can be found. For example, using the GUI: What (if any) variables are currently in your workspace? What files are in your current directory? What was your last but one command? Use the commands length and size to check your variables Investigate the help available in matlab. Using the help: Find the function for standard deviation. Find the function for variance. Read the help on the standard deviation and variance functions Apply these two functions to the matrix a=[1:10] Do you get 3.0277 and 9.1667? Useful videos

https://www.youtube.com/watch?v=VheLmG7rh9w&feature=youtu.be (MATLAB environment 23min) https://www.youtube.com/watch?v=ZEOubc5i_n8 (MATLAB as a calculator 14min) https://www.youtube.com/watch?v=Aco4yDvse0I (Syntax and Semantics 5min) https://www.youtube.com/watch?v=_wxLhQy__6o (the MATLAB help system 8min)

https://www.youtube.com/watch?v=3T4reHaJtXE (Introduction to Matrices and Operators 11min) https://www.youtube.com/watch?v=i1z3Z0LlPSg (The Colon Operator 8min)

Recently Viewed Presentations

  • Animal Diversity - Edl

    Animal Diversity - Edl

    Animalia. Kingdom extends far beyond dogs, birds, and humans - vast array of diversity covers 1.3 million animal species - definition is not straight forward because of exceptions to every criterion
  • EU Proposal Business Intelligence to Quickly Model Data

    EU Proposal Business Intelligence to Quickly Model Data

    % of accepted advice (Trust). Saving percentage (Expectation bias). Not influential: External temperature. Average temperatures\fan. Accepted deltas. Prediction of drivers reactions. Ariel Rosenfeld et al. AAAI-15 (WAIT-15 workshop) @ Austin, TX USA. January 2015
  • The Kelvin Scale - ScienceGeek.net

    The Kelvin Scale - ScienceGeek.net

    The Nature of Gases. Gases expand to fill their containers. Gases are fluid - they flow. Gases have low density. 1/1000 the density of the equivalent liquid or solid
  • January 6, 2019 Welcome to OPC Cambridge Chimes

    January 6, 2019 Welcome to OPC Cambridge Chimes

    Sheffet. STATEMENT ON ORDINATION AND INSTALLATION. The Session also installs as the Church Trustees: Wayne . Bulan, Eileen Smith, and . Jeff . Heitzenrater. REAFFIRMATION OF THEBAPTISMAL COVENANT. Minister
  • PERS-43 Overview

    PERS-43 Overview

    FRS. IN-RESIDENCE GRADUATE EDUCATION. CAREER WINDOWS. Tour lengths vary based on individual / community requirements. Career Path. Warfare Area Focus.
  • Non-Riemannian geometry, Born-Infeld models and trace free ...

    Non-Riemannian geometry, Born-Infeld models and trace free ...

    unimodular. gravity and general relativity is given by the arbitrary value of . lamda. 3) The fact that the determinant of the metric is fixed has clearly profound consequences on the structure of given theory. First of all, it reduces...
  • The energy crisis: Implications for developing countries Short

    The energy crisis: Implications for developing countries Short

    The energy crisis: Implications for developing countries Short courses for Permanent Missions in Geneva Adapting to the new energy realities: trade and development perspectives
  • THE RULES - Wolstanton High School

    THE RULES - Wolstanton High School

    The power of PiXL in the palm of your hands. The PMA is an online assessment system that identifies your strengths and areas to develop in Mathematics. The app provides therapy videos to deal with any issues and the means...