Title: Matlab Class 8 Xiaotao Su, Ph.D. Visual Psychophysicist
1Matlab Class 8Xiaotao Su, Ph.D.Visual
Psychophysicist IT Group LeaderRutgers Center
for Cognitive Science (RuCCS)andChris Kourtev
2Small Pieces
- Starting with matlab version 7.0 you can execute
small chunks of code - This is called cells (nothing to do with cell
arrays) - mark off the beginning and end of cell region
- Cell regions are seen as yellow
- Pressing ctrl/cmd return causes the workspace
to execute the command in the active cell
If you do not see a yellow region, in the
menu bar select Cell-gtEnable Cell Mode
3Creating a vector of numbers
- This is similar to the way you create an
iteration for a for loop - x (020) ----gt x 0, 1, 20
- x (0.10.11) ---gt x 0.1, 0.2, , 1.0
- x (0220) ----gt x 0, 2, 4, 20
- x (0220) ---gt vertical vector like the
previous one
4discrete distributions and selecting segments
- you can use something like x(020) to specify a
discrete distribution of 21 different
possibilities - to select the first 6 elements you could sayy
x(16) ---gt y would be 0, 1 5 - Or to select the last 4 elements
- y x(end-3end) ---gt y would be 17, 18, 19, 20
5y binopdf(x(16), 5, .8)
- used to generate a binomial distribution function
the case in which one flips a weighted coin - .8 probability of getting heads
- there are 6 possibilities (0 heads, 1 head 5
heads - y gives the probability of each of these outcomes
This function requires the stats toolbox
6Plotting
- Subplot will create a figure window for plotting
- subplot (2, 1, 2) will say we are going to create
a (2 row, 1 column, ) figure. The last number
specifies that we are currently going to write in
the second element of this figure. - plot(y) will create a graph on currently chosen
section of the figure (specified by the third
parameter in subplot)
7setting up the plot
- xlim(1 5) sets the limits of the x axis
- bar(y) will use a bar graph
- ylabel(Probability of n)
- xlabel(N)
- title(Discrete distributions should be plotted
as histograms)
8more then one figure?
- figure opens a new figure window
- All new subplots will go to the new figure
- subplot(2, 1, 1)
- bar(y)
- xlim(1 21) we want 21 possibilities
9more then one graph on a plot
- hold on tells matlab to keep the previous graph
and draw a new one on top - stairs(cumsum(y), r)
- cumsum(y) lt--- each value is the sum of previous
values of y - r makes the graph line red
- stairs will plot it as a staircase
- hold off now any future graph additions will
clear previous graphs on this plot - legend(distribution, cumsum)
10Using images in experiments
- Images are stored in matlab as
- Width x Height x 3 matrix
x
R
G
y
B
11file -gt matrix and drawing it on the screen
- img imread(winter.jpg, jpg)
- You draw an image just like you would a
rectangle - Screen(window, PutImage, img, 0, 0, 200,
200)
12Image info and structures
- img_info imfinfo(winter.jpg)
- img_info.Width will be the width of the image
- screen(window, PutImage, img,
- 0, 0, img_info.Width, img_info.Height)
13Making sounds
- A sound is a 2 x N matrix where N is the number
of bits that make up the sound file and the two
channels are for left and right
14Making sounds
- sound, samplerate, samplesize
- wavread(feedback.wav)
- sound sound
- Snd(Play, sound, samplerate, samplesize)
Tip To make your own wav files I recommend
using an application called audacity http//audac
ity.sourceforge.net/