# 1. Introduction

This chapter covers the following topics:

• 1.1 Getting oriented
• 1.2 Getting an overview of this book
• 1.3 Understanding computer architecture
• 1.4 Programming principles
• 1.5 Deciding if a program is needed and whether you should write it
• 1.6 Being as clear as possible about what your program should do
• 1.7 Working incrementally
• 1.8 Being open to negative feedback
• 1.9 Programming with a friend
• 1.10 Writing concise programs
• 1.11 Writing clear programs
• 1.12 Writing correct programs
• 1.13 Understanding how the chapters of this book are organized
• 1.14 Using the website associated with this book
• 1.15 Obtaining and installing MATLAB
• 1.16 Acknowledging limits

# 2. Interacting With MATLAB

This chapter covers the following topics:

• 2.1 Using MATLAB’s windows
• 2.2 Using the Command window
• 2.3 Writing tiny programs in the Command window
• 2.4 Allowing or suppressing outputs by omitting or including end-of-line semi-colons
• 2.5 Correcting errors in the Command window
• 2.6 Writing, saving, and running larger programs as scripts (.m files)
• 2.7 Running and debugging MATLAB programs
• 2.8 Keeping a diary
• 2.9 Practicing interacting with MATLAB

# 3. Matrices

This chapter covers the following topics:

• 3.1 Creating matrices
• 3.2 Specifying elements of matrices
• 3.3 Concatenating matrices
• 3.4 Determining the size of matrices
• 3.5 Transposing or reshaping matrices
• 3.6 Creating matrices with short-hand methods
• 3.7 Checking the status of matrices
• 3.8 Clearing and emptying matrices
• 3.9 Practicing with matrices

# 4. Calculations

This chapter covers the following topics:

• 4.1 Adding, subtracting, multiplying, and dividing values, and raising values to a power
• 4.2 Using built-in functions to compute the square root, remainder, absolute value, logarithms, and exponentiation
• 4.3 Ordering calculations
• 4.4 Generating random numbers
• 4.5 Performing statistical calculations to obtain the sum, mean, standard deviation, variance, minimum, maximum, correlation, and least-squares fit
• 4.6 Performing statistical calculations with missing data
• 4.7 Calculating with matrices
• 4.8 Using matrix algebra
• 4.9 Sorting arrays
• 4.10 Rounding values, and finding their floor and ceiling
• 4.11 Generating magic squares and calendars
• 4.12 Practicing calculations

# 5. Contingencies

This chapter covers the following topics:

• 5.1 Using the if ... else ... end construct
• 5.2 Using the switch ... case ... end construct
• 5.3 Using the for ... end construct
• 5.4 Using the while ... end construct and escaping from run-away loops
• 5.5 Vectorizing rather than using for ... end
• 5.6 If-ing instantly
• 5.7 If-ing instantly once again and finding indices of satisfying values
• 5.8 Applying contingencies: Constrained random sequences and Latin squares
• 5.9 Practicing contingencies

# 6. Input-Outputs

This chapter covers the following topics:

• 6.1 Copying and pasting data by hand
• 6.2 Getting input from a user and displaying the result
• 6.3 Pausing
• 6.4 Recording reaction time and other delays with tic ... toc
• 6.5 Formatting numbers for screen outputs
• 6.6 Assigning arrays of literal characters (strings) to variables
• 6.7 Controlling file print formats
• 6.8 Writing data to named files
• 6.9 Writing text to named files
• 6.10 Checking and changing the current directory
• 6.11 Reading data saved as plain text from named files
• 6.13 Taking precautions against overwriting files
• 6.15 Learning more about input and output
• 6.16 Practicing input-output

# 7. Data Types

This chapter covers the following topics:

• 7.1 Identifying strings, numbers, and logical values (Booleans)
• 7.2 Converting characters to numbers and vice versa
• 7.3 Creating, accessing, and using cell arrays
• 7.4 Creating and accessing structures
• 7.5 Searching and modifying strings
• 7.6 Applying data types
• 7.7 Practicing data types

# 8. Modules and Functions

This chapter covers the following topics:

• 8.1 Taking a top-down approach to programming by using modules
• 8.2 Writing and using general-purpose functions
• 8.3 Getting multiple outputs from functions
• 8.4 Passing multiple input arguments to functions
• 8.5 Creating multiple functions in a file
• 8.6 Calling functions properly
• 8.7 Exploiting recursive functions
• 8.8 Drawing on previously defined functions versus creating your own
• 8.9 Bypassing the constraints of functions
• 8.9 Practicing modules and functions

# 9. Plots

This chapter covers the following topics:

• 9.1 Deciding to plot data and, for starters, plotting a sine function
• 9.2 Controlling axes
• 9.3 Controlling the appearance of plotted points and lines
• 9.4 Having more than one graph per plot and more types of points and lines
• 9.5 Getting and setting properties of plotted points
• 9.6 Adding xlabels, ylabels, and titles
• 9.9 Fitting curves
• 9.10 Creating and labeling subplots; turning grids, boxes, and axes on and off
• 9.11 Exploiting matrix assignments to merge subplots
• 9.12 Getting and setting properties of axes
• 9.13 Plotting data points with error bars
• 9.14 Generating polar and compass plots
• 9.15 Generating histograms
• 9.16 Generating bar graphs
• 9.17 Saving, exporting, and printing figures
• 9.18 Generating other kinds of graphs and getting and setting figure properties
• 9.19 Practicing plots

# 10. Lines, Shapes, and Images

This chapter covers the following topics:

• 10.1 Generating lines
• 10.2 Forming and filling shapes
• 10.4 Generating your own images
• 10.5 Clicking in figure windows to add graphics, add text, or record responses
• 10.6 “Stairing”
• 10.7 Generating three-dimensional bar graphs
• 10.8 Plotting in three dimensions
• 10.9 Plotting “above” a meshgrid
• 10.10 Plotting “meshy” data
• 10.11 “Surfing” the “web”
• 10.12 Changing points of view
• 10.13 Generating contours
• 10.14 Checking your understanding of meshgrid-based graphing
• 10.15 Generating rectangular solids
• 10.16 Generating spheres and cylinders
• 10.17 Generating ellipsoids
• 10.18 Practicing plots

# 11. Animation and Sound

This chapter covers the following topics:

• 11.1 Animating by replacing successive images
• 11.2 Watching comets
• 11.3 Animating by drawing now
• 11.4 Making movies
• 11.5 Saving movies
• 11.6 Reading and running previously saved movies
• 11.7 Playing beeps
• 11.9 Controlling volume
• 11.10 Staggering or overlapping sounds and delaying sounds
• 11.11 Controlling volume while staggering or overlapping sounds
• 11.12 Creating your own sound files computationally
• 11.13 Writing and reading files for sound
• 11.14 Learning more about sound-related functions
• 11.15 Practicing animation and sounds

# 12. Enhanced User Interaction

This chapter covers the following topics:

• 12.1 Getting less “clunky”
• 12.2 Creating graphic user interfaces (GUIs)
• 12.3 Using built-in user interface functions
• 12.4 Writing code for user interface functions
• 12.5 Prototyping user interfaces using GUIDE
• 12.6 Recording user interactions
• 12.7 Practicing interfaces and interactions

# 13. Psychtoolbox

This chapter covers the following topics:

• 13.1 Introducing Psychtoolbox
• 13.2 Installing Psychtoolbox
• 13.3 Writing a simple Psychtoolbox program
• 13.4 Using Psychtoolbox documentation
• 13.5 Changing fonts and font sizes
• 13.6 Adding shapes to a display
• 13.7 Adding textures and images to a display
• 13.8 Displaying stimuli sequentially with precise timing
• 13.9 Collecting keyboard input
• 13.10 Monitoring keyboard input while doing other things
• 13.11 Collecting a response string
• 13.12 Collecting mouse data
• 13.13 Creating an animation with moving dots
• 13.14 Making things transparent
• 13.15 Testing the Simon effect with Psychtoolbox
• 13.16 Exploring Psychtoolbox further
• 13.17 Recovering from Psychtoolbox program crashes and infinite loops
• 13.18 Problems

# 14. Debugging

This chapter covers the following topics:

• 14.1 Debugging using error messages and breakpoints
• 14.2 Using temporary feedback for debugging
• 14.3 Interpreting error messages
• 14.4 Using graphic output for debugging
• 14.5 Practicing debugging