SECOND EDITION MATLAB for Behavioral Scientists David A.Rosenbaum. Jonathan Vaughan,Brad Wyble
Contents Preface ix Acknowledgements xi About the Authors 1 Introduction 1 2 Interacting With MATLAB 22 3 Matrices 38 4 Calculations 6 5 Contingencies 99 6 Input-Output 128 7 Data Types 8 Modules and Functions 182 9 Plots 10 Lines,Shapes,and Images 248 11 Animation and Sound 12 Enhanced User Interaction 13 Psychtoolbox 14 Debugging 355 15 Going On 370
vii Preface ix Acknowledgements xii About the Authors xiii 1 Introduction 1 2 Interacting With MATLAB 22 3 Matrices 38 4 Calculations 62 5 Contingencies 99 6 Input-Output 128 7 Data Types 156 8 Modules and Functions 182 9 Plots 208 10 Lines, Shapes, and Images 248 11 Animation and Sound 287 12 Enhanced User Interaction 304 13 Psychtoolbox 323 14 Debugging 355 15 Going On 370 Contents
vi Contents References 375 Commands Index 377 Name Index 381 Subject Index 382
viii Contents References 375 Commands Index 377 Name Index 381 Subject Index 382
Preface The first edition of MATLAB for Behavioral Scientists(published in 2007)was the result eeooeoeoe t write the ng stu ctive neuroscience,economics,a nd s mas much as scientists in other fields.They ohe able t n to do r the nally spe ing of granting agencies to pay other to program for them To give some examples a behavioral scientist-a behavioral economist.say-wishing to model decision making should be able to roll up her sleeves and observed and expected data in the way she prefers.A personality p in designing a new questionnaire requiring a special computer interface should be able to oursue that aim A svchotherapist wanting to model changing relations between mem- bers of a family should be able to characterize that process with custom-made animations that show network links with dynamically changing thicknesses and colors.growing and shrinking over time,if that's what she wants.A cognitive psychologist interested in setting up and conducting behavioral experiments should be able to create any kind of stimuli and response recording capabilities he or she cares to,not being limited by what's possible with off-the-shelf commercial products. This book is meant to help behavioral scientists(and especially students entering this field) to do these things.The authors of this book assume you have no prior familiarity with com- puter programming.and we assume you have no knowledge of mathematics beyond wha ble.our aim is to draw you in and help you feelcom in high sch he text is me at first seem foreign and maybe even scary. y as well a will appen b e.I es a tough hide to b ve you ha d has first but the med has s feel k to program.No one does! Programming needn't be unpleasant.however.The attitude to have is to keep an open mind about the value of mistakes.If you treat errors as windows for improvement,you will learn a lot.Availing yourself of that learning,when you see a program work and especially ix
ix Preface The first edition of MATLAB for Behavioral Scientists (published in 2007) was the result of a rebellious thought. The prevailing view before then was that most behavioral scientists shouldnÊt or couldnÊt write their own computer programs. This irked the first author, who decided to pursue the notion that all behavioral scientists, including students in the relevant fields (psychology, cognitive and affective neuroscience, economics, and so on), could and should learn to program for themselves. Behavioral scientists need to be able to program as much as scientists in other fields. They need to be able to program to do whatever they want, computationally speaking, without having to rely on the kindness of strangers or the largesse of granting agencies to pay others to program for them. To give some examples, a behavioral scientist·a behavioral economist, say·wishing to model decision making should be able to roll up her sleeves and graph data showing observed and expected data in the way she prefers. A personality psychologist interested in designing a new questionnaire requiring a special computer interface should be able to pursue that aim. A psychotherapist wanting to model changing relations between members of a family should be able to characterize that process with custom-made animations that show network links with dynamically changing thicknesses and colors, growing and shrinking over time, if thatÊs what she wants. A cognitive psychologist interested in setting up and conducting behavioral experiments should be able to create any kind of stimuli and response recording capabilities he or she cares to, not being limited by whatÊs possible with off-the-shelf commercial products. This book is meant to help behavioral scientists (and especially students entering this field) to do these things. The authors of this book assume you have no prior familiarity with computer programming, and we assume you have no knowledge of mathematics beyond what is generally learned in high school. The text is meant to be as friendly and encouraging as possible. Our aim is to draw you in and help you feel comfortable within a domain that may at first seem foreign and maybe even scary. Programming can be humbling. If you set out to learn to program, you should prepare yourself emotionally as well as intellectually for what will happen because you will be dealing with an unfeeling machine. It takes a tough hide to believe you have a program that does what you want, only to discover that the program doesnÊt run, generates unexpected results, or produces outputs that seem reasonable at first but then turn out to be wrong. Everyone who has programmed has gone through this, including the authors of this book, so donÊt feel like you need to be able to program perfectly. No one does! Programming neednÊt be unpleasant, however. The attitude to have is to keep an open mind about the value of mistakes. If you treat errors as windows for improvement, you will learn a lot. Availing yourself of that learning, when you see a program work and especially
Preface when it does something that,to your knowledge,has not been done before,can let you feel rightly proud of your achievement. There are many computer programming languages.Why is this book about MATLAB? MATLAB(short for Matrix Laboratory),is a commercial product of a company called The Math Works (Natick.Massachusetts).for which we authors do not work and have no com mercial connection.The following.therefore,can be taken as our honest opinion of their product:MATLAB is a simple yet powerful language for computer programming.It has an active community of users,engaged in many branches of science and engineering.One of M s m ractive igh-level comm lations with large as well as sm d fo ngpublic AB e collectio or the pr on of espo platf .runs o Cs.Ma ng ntist vith in oth is have therefor MATLAB ing to the hea alth an it is to learn matlab in the futu e.You will be art of thatacti ou choose to dive into the material provided here How did it come to n ss that there is a second edition of this book?As is always true of a second edition,its predecessor was successful enough to keep the work alive.but changes in the field suggested a face-lift was needed.Among the needed changes was the appear- ance of other MATLAB books for psychologists and neuroscientists(Fine Boynton. 2013:Madan,2014:Wallisch et al.2009).which are welcome additions.though they are different in style,tone,level of coverage,and organization from the first edition of this book(but not so perfect,in our view,that they obviate this second edition). As the author of the first edition (Rosenbaum,2007)contemplated the second edition,he realized that the process of revising and updating the book would benefit from the involve- ment of his long-time friend and collaborator,Jonathan Vaughan,the James L.Ferguson Professor of Psychology and Neuroscience at Hamilton College.Jon has decades of experi- ence with computer programming.He has served as the editor of Behavior Research Meth wed publication of the Psychonomic Society irst a hor bas AB from Jon.He continued to leamn from Jon in an【 map out the ways the e sec ong th ag o we (1)All kn errors in the e firstediti would b cted more gging;G ic AB (4 blems woulda with do n the hook's com/780415535946)rather than in the back of the book to allow for mor pdating of the nd additio eds and 5h ould he a tutorial on designing gra GUIs,which enable a user to interact with a program using g within MATLAB:(6)there would be a tutorial in designing experiments using Psychtool- box,a freely available MATLAB toolbox that is specifically geared to behavioral science
x Preface when it does something that, to your knowledge, has not been done before, can let you feel rightly proud of your achievement. There are many computer programming languages. Why is this book about MATLAB? MATLAB (short for Matrix Laboratory), is a commercial product of a company called The MathWorks (Natick, Massachusetts), for which we authors do not work and have no commercial connection. The following, therefore, can be taken as our honest opinion of their product: MATLAB is a simple yet powerful language for computer programming. It has an active community of users, engaged in many branches of science and engineering. One of MATLABÊs most attractive features is that it offers high-level commands for performing calculations with large as well as small data sets and for generating publication-quality graphics. Another attraction of MATLAB is that it allows for the presentation of stimuli and the collection of responses with precise timing. Yet another attraction is that MATLAB is platform-independent. It runs on PCs, Macs, and Linux machines. For these and other reasons, MATLAB is a very good language for behavioral scientists. A growing number of behavioral scientists, along with neuroscientists, engineers, and investigators in other disciplines, have therefore chosen to learn MATLAB. Owing to the health and vitality of the MATLAB programming community, it is likely that more and more people will want to learn MATLAB in the future. You will be part of that active community if you choose to dive into the material provided here. How did it come to pass that there is a second edition of this book? As is always true of a second edition, its predecessor was successful enough to keep the work alive, but changes in the field suggested a face-lift was needed. Among the needed changes was the appearance of other MATLAB books for psychologists and neuroscientists (Fine & Boynton, 2013; Madan, 2014; Wallisch et al., 2009), which are welcome additions, though they are different in style, tone, level of coverage, and organization from the first edition of this book (but not so perfect, in our view, that they obviate this second edition). As the author of the first edition (Rosenbaum, 2007) contemplated the second edition, he realized that the process of revising and updating the book would benefit from the involvement of his long-time friend and collaborator, Jonathan Vaughan, the James L. Ferguson Professor of Psychology and Neuroscience at Hamilton College. Jon has decades of experience with computer programming. He has served as the editor of Behavior Research Methods, Instruments, & Computers , a peer-reviewed publication of the Psychonomic Society. The first author basically learned MATLAB from Jon. He continued to learn from Jon in preparing this second edition. When Jon agreed to join in, he and David began to map out the ways the second edition would differ from the first. Among the things they agreed to were the following: (1) All known errors in the first edition would be corrected; (2) more would be said about debugging; (3) more problems would be given, including problems that would help students confront very basic issues in the rudiments of MATLAB; (4) solutions to selected problems would appear with downloadable code on the bookÊs new website ( www.rout ledge.com/9780415535946 ) rather than in the back of the book to allow for more extensive code, updating of the programs if necessary, and addition of new programs as needs and curiosities arose; (5) there would be a tutorial on designing Graphical User Interfaces, or GUIs, which enable a user to interact with a program using graphics to run experiments within MATLAB; (6) there would be a tutorial in designing experiments using Psychtoolbox, a freely available MATLAB toolbox that is specifically geared to behavioral science