Mauro Borgo.Alessandro Soranzo Massimo Grassi MATLAB for Psychologists Springer
Mauro Borgo ● Alessandro Soranzo Massimo Grassi MATLAB for Psychologists
Preface Psychological researchers should possess several skills,and one of them is surely creativity.Creativity is needed at several key points of the research process,such as in and designing an exp ment Creativity drives mbe a revea their full potentia. Much of this er vity I is now expres ed through a comput er program.For exam use specific software that has been dedicated to that particular job.This software might,however,be a hindrance to creativity.preventing it from permeating research This is because in the majority of cases,software is designed to satisfy the average user and it is not flexible enough to meet specific needs. In this sense.MATLAB is exactly the other side of the coin.When we first open the software,the lack of a y he fr ng:at a first e program nay se em di ing users first approach eriments and n the ro major advantage:we do not have to adapt our needs to the software;it is the soft- ware that adapts to our needs. MATLAB is an extremely powerful research tool.By means of this single soft ware tool we can control every step of our research.We can create stimuli of any kind (e.g.pictures,sounds).and we can program psychological experiments,calcu- late statisti simulations. and do kind o signal processin control and very con step of computer progrm.Moreover.knowledg of MATLAB will help you to find a postdoc in experimental psychology after com- pleting the Ph.D.In many cases,research groups look for researchers with good MATLAB programming skills. The current text is written to help the newcomer in using MATLAB for research page http://www.psy.unipd.it -gT assi/m atlab book html vii
vii Psychological researchers should possess several skills, and one of them is surely creativity. Creativity is needed at several key points of the research process, such as in creating experimental stimuli and planning and designing an experiment. Creativity drives good data analysis, so that numbers can reveal their full potential. Much of this creativity is now expressed through a computer program. For example, in planning and designing a psychological experiment and in analyzing data, we use specifi c software that has been dedicated to that particular job. This software might, however, be a hindrance to creativity, preventing it from permeating research. This is because in the majority of cases, software is designed to satisfy the average user and it is not fl exible enough to meet specifi c needs. In this sense, MATLAB is exactly the other side of the coin. When we fi rst open the software, the lack of a graphical interface may be frustrating: at a fi rst glance, the program may seem diffi cult to use. This book is aimed at helping users in their fi rst approaches to this software, to aid them in programming their psychological experiments and consequently in liberating their creativity. And this is MATLAB’s major advantage: we do not have to adapt our needs to the software; it is the software that adapts to our needs. MATLAB is an extremely powerful research tool. By means of this single software tool we can control every step of our research. We can create stimuli of any kind (e.g., pictures, sounds), and we can program psychological experiments, calculate statistics, run simulations, and do any kind of signal or biosignal processing. In brief, the fl exibility of this software lets us to control and customize every conceivable step of our research requiring a computer program. Moreover, knowledge of MATLAB will help you to fi nd a postdoc in experimental psychology after completing the Ph.D. In many cases, research groups look for researchers with good MATLAB programming skills. The current text is written to help the newcomer in using MATLAB for research in experimental psychology. However, the content can be transferred to any application. The reader can fi nd the scripts written in this book at the following web page: http://www.psy.unipd.it/~grassi/matlab_book.html Preface
Contents 1 Basic Operations. Variables. Thinking in a Matrix Way 8 Operations. 15 Summary. 17 Exercises A Brick for an Experiment 2 Data Handling 25 Types of Variables (Logical Values.Strings,NaN.Structures.Cells). Logical Variables. Strings. NaN. Cells Import/Export. Summary. Exercises. 43 A Brick for an Experiment. 44 Read the Results. 44 Reference 46 Suggested Readings 46 3 Plotting Data Plot Dat Control the Plot's Objects:Labels,Legend,Title. Subplot:Multiple Plots in One Figure. 52 3-D Plots Printing and Saving Images. 58
xi 1 Basic Operations . 1 Variables . 5 Thinking in a Matrix Way . 8 Operations . 15 Summary . 17 Exercises . 18 A Brick for an Experiment . 20 References . 23 Suggested Readings . 23 2 Data Handling . 25 Types of Variables (Logical Values, Strings, NaN, Structures, Cells) . 25 Logical Variables. 25 Strings . 31 NaN . 35 Structures . 35 Cells . 38 Import/Export . 40 Summary . 42 Exercises . 43 A Brick for an Experiment . 44 Read the Results . 44 Reference . 46 Suggested Readings . 46 3 Plotting Data . 47 Plot Data . 47 Control the Plot’s Objects: Labels, Legend, Title. . 50 Subplot: Multiple Plots in One Figure . 52 3-D Plots . 56 Printing and Saving Images . 58 Contents
Contents Handle Graphics. 86 Exercises 6 A Brick for an Experiment. Plot the Results. Reference 65 Suggested Readings 4 Start Programming M-Script and Function Control Flow Statements Cycles and Conditionals:If. Switch Case. ForL00ps. While Break. 81 Try-Catch ops Versus Matrices and If Versus Logicals. Function Change the Number of Inputs and Outputs More on Data Import/Export:Script Examples. Analysis . Guidelines for a Good Programming Style. 96 Writing Code. 96 Debug 98 100 Analysis 0Q9 References Suggested Readings. 106 5 A Better Sound. 107 Generate a sound 107 Multiple Sou 112 Man 's Level with Di ferent Waveforms. A Sound's Envelope. Sound Filtering. Sound Analysis 123 Summary 12s Exercises 125
xii Contents Handle Graphics . 58 Summary . 61 Exercises . 62 A Brick for an Experiment . 64 Plot the Results . 64 Reference . 65 Suggested Readings . 65 4 Start Programming . 67 M-Scripts and Functions . 67 Control Flow Statements. 70 Cycles and Conditionals: If . 70 Switch Case . 72 For Loops . 74 While . 78 Break . 81 Try–Catch . 82 Loops Versus Matrices and If Versus Logicals . 82 Functions . 83 Scope of Variables . 86 Change the Number of Inputs and Outputs . 87 More on Data Import/Export: Script Examples . 90 Analysis . 95 Guidelines for a Good Programming Style . 96 Writing Code . 96 Debug . 98 Summary . 100 Exercises . 101 A Brick for an Experiment . 102 Analysis . 104 References . 106 Suggested Readings . 106 5 A Better Sound . 107 Generate a Sound . 107 Multiple Sounds . 112 Manipulating a Sound’s Level . 114 Match the Level of Sound with Different Waveforms . 115 Stereophonic Sounds for ITD and ILD . 116 A Sound’s Envelope . 118 Sound Filtering. 120 Sound Analysis. 123 Summary . 125 Exercises . 125
Contents xi A Brick for an Experiment. 126 References. Suggested Readings. 128 6 Create and Proccess Images Images Basics. Importing and Exporting Images. Display Images. Basic Manipulation of Images. 135 Point Operations. 136 Intensity Transformation 136 138 ges of th e Image 144 Advance Image Processing. Creating Images by Computation 44 149 Exercises. 150 References Suggested Readings 152 7 Data Analysis 53 De easures of Central Tendency Measures of Dispersion. Bivariate and Multivariate Descriptive Statistics. Covariance. Simple and Multiple Linear Regression. 156 Generalized Linear Model 160 Inferential Statistics 162 ANOVA 66 Nonparametric Statistics Categorical Data. Ordinal Data. Signal-Detection Theory(STD)Indexes 182 Summary. 184 Exercises 185 ABrick for an Experiment 186 References 1 Suggested Readings
Contents xiii A Brick for an Experiment . 126 References . 127 Suggested Readings . 128 6 Create and Proccess Images . 129 Images Basics . 129 Importing and Exporting Images . 132 Display Images . 134 Basic Manipulation of Images . 135 Point Operations . 136 Intensity Transformation . 136 Windowing . 138 Neighborhood Processing . 140 The Edges of the Image . 144 Advanced Image Processing . 144 Creating Images by Computation . 144 Summary . 149 Exercises . 150 References . 151 Suggested Readings . 152 7 Data Analysis . 153 Descriptive Statistics . 153 Measures of Central Tendency . 153 Measures of Dispersion . 154 Bivariate and Multivariate Descriptive Statistics . 155 Covariance . 156 Simple and Multiple Linear Regression . 156 Generalized Linear Model . 160 Inferential Statistics . 162 Parametric Statistics . 162 t-Test . 163 ANOVA . 166 Nonparametric Statistics . 177 Categorical Data . 177 Ordinal Data . 179 Signal-Detection Theory (STD) Indexes . 182 Summary . 184 Exercises . 185 A Brick for an Experiment . 186 References . 187 Suggested Readings . 187