Title: Introduction To Matlab Class 3
1Introduction To MatlabClass 3
- Instructors Hristiyan (Chris) Kourtev
- and Xiaotao Su, PhD
2Variables
- Integers m 5 5
- Doubles (Floating pt) n 7.382
- Character strings c1 beep b, e,
e, p c2 4 - Arrays of numbers arr1 4, 5, 8, m arr2
m, n, 5.6, 0 - Arrays of strings str1 c1 blob same
dimen. - Concatenating arrays of numbers arr3 arr1,
arr2 - Concatenating strings str2 c1,c2
- Matrices mat1 4, 5 6, 7 mat2 arr1
arr2 same dimen. - Cell Arrays (later on)
3Boolean Expressions
- Boolean operands
- Boolean expressions either return 1 for true
e.g. 5 5 or 0 for false e.g. 5 gt 9 - Put expressions in parentheses so they get
evaluated firste.g. 0 (4lt5)
gt gt lt lt
equals not equal greater than greater than or equal to less than greater than or equal to and or
4Loops (for and while)
- For loopfor index fromto do
somethingend - While loopwhile(condition) do something
change something that affects value of
conditionend
5Loops (for and while) -- examples
- max_loops 5
- for index 1max_loops
- disp(index)
- end
- counter 1
- while(counter lt max_loops)disp(counter)
- counter counter 1
- end
nested loop example for k 1max_loops disp(k1
) for m 13 disp(m) end disp(k2) en
d outputs k1 m m m k2 k1 m m m k2 k1 mmm
k2 k1 m m m k2 k1 mmm k2
6Commonly used functions
- rand - generates a random decimal number between
0 and 1e.g. 0.3456 or 0.9993 or 0.0013 etc - ceil(num) returns the next integer bigger than
the inpute.g. ceil(5.56) ?6 or ceil(2.1) ? 3 or
ceil(6) ? 6 - floor(num) returns the next integer, smaller
than the inpute.g. floor(0.9) ? 0 or floor(-0.1)
? -1 - To generate a random number between 0 20
ceil(rand20)
7Commonly used functions -- continued
- m 1, 2, 3, 4 n 1, 2, 3, 4 5, 6, 7, 8 k
9 8 0 - length(mat) returns the length of a vector or a
matrixe.g. length(m) ? 4, length(n) ? 4,
lenth(k) ? 3 - size(mat,dim) returns all the dimensions of a
matrix/vectore.g. size(m) ? 1, 4, size(n) ?
2, 4, size(k) ? 3, 1, size (n, 2) ? 4
8Multiple Input/Output Functions
- Functions can have more than one input and more
than one outpute.g. s size(mat, dim) - Storing returned values in 2 or more separate
variablese.g. x, y size(mat) - Storing returned values in a vector/cell
arraye.g. vals size(mat)
9Collecting User Input Using it
- Take input from keyboardnum1input('what is the
first number?') - Validation checks - isstr(var)- isnum(var)
- Converting from strings to numbers and back-
num2str(var)- str2num(var)
10Screen drawing and psychtoolbox
Origin
Coordinates are measured in
pixels.
X ----------- Positive -gt
Y
Positive
Max X and Y
11Basic Display Loop in PsychToolbox (PTB)
- Open Window
- while some condition is true do the following
- draw something
- make some changes
- repeat
- Close screen
12Basic Display Loop in PTB (code)
- draw_stuff.m
-
- Draws stuff on the screen
- clear all
- try
- which_screen0
- bg_color 0, 0, 0
- window_ptr, screen_dimensions
- Screen(which_screen, 'OpenWindow',bg_color)
- shape_dimensions screen_dimensions/2
- tic
-
while (toc lt 5) move shape
shape_dimensions shape_dimensions
0,5,0,5 draw shape
Screen(window_ptr, 'FillRect', 0,0,255,
shape_dimensions) flip buffer and
repeat Screen(Flip, window_ptr) end cl
ear Screen catch clear Screen end
13Moving Shapes around the Screen
- Shapes are defined by an array of numbers e.g.
sh1 10, 15, 40, 45 x1, y1, x2, y2 - To move a shape in the x direction by 5 pixels we
need to increment both x coordinates by 5e.g.
sh1(1) sh1(1) 5 sh1(3) sh1(3) 5 or use
another array sh1 sh1 5, 0, 5, 0 - Same for moving in the y direction
- Increment both if you want to move diagonally
14Task 1 Mousing Around
- Draw a green circle of radius 30 pixels, whose
location on the screen is controlled by the mouse - Change the color of the circle to red when a
button is clicked and back to green when released - Hints You will need to use the following
functions - Get input from the mousemouse_x, mouse_y,
buttons GetMouse(window_ptr) - Hide and show the windows cursorHideCursor,
ShowCursor - Draw a circleScreen(window, 'FillOval',
dot_color, cursor_dim) - If sum(buttons)gt0, a button has been clicked
15- mousingAround.m
- clear all
- try
- which_screen0
- bg_color 0, 0, 0
- window_ptr, screen_dimensions
- Screen(which_screen, 'OpenWindow',
bg_color) - dot_r 20 radius of cursor
- HideCursor
- green 0, 255, 0
- red 255,0, 0
- tic
while(toclt5) mouse_x, mouse_y, buttons
GetMouse if(sum(buttons)gt0) dot_color
red else dot_color green end cursor
mouse_x-dot_r, mouse_y-dot_r, ...
mouse_xdot_r, mouse_ydot_r Screen(window_pt
r, 'FillOval', dot_color, cursor) Screen('Flip',
window_ptr) end clear Screen ShowCursor catch
clear Screen ShowCursor end
16Skip Sync Tests
- Add to your program before opening the new
screenScreen('Preference','SkipSyncTests', 1)
17Direction/Velocity Vectors
- A vector has both magnitude and direction
- Direction x,y
- Magnitude v v(x2y2) sqrt(x2 y2)
Pythagorean
x
v
y
x
18Detecting Collisions With Borders
- rect1 10, 10, 50, 50 x1, y1, x2, y2
- screen_dimensions 0, 0, 1280, 1024
- Collision w/ top border if rect1(2) lt
screen_dimensions(2) - Collision w/ left border if rect1(1) lt
screen_dimensions(1) - Collision w/ bottom border if rect1(4) gt
screen_dimensions(4) - Collision w/ right border if rect1(3) gt
screen_dimensions(2)
19Task 2 Bouncing off the walls
- Modify your existing program to add 2 blue
squares that start off in a random direction at
speed 5 and 7 pixels respectively and bounce off
the walls - Hints
- Will need to use direction vectors
- Will need to use the Pythagorean theorem to
calculate the direction vectors - If a square bumps into the left or right walls,
invert (multiply by -1) the x component of its
velocity vector - If a square bumps into the left or right walls,
invert (multiply by -1) the y component of its
velocity vector
20Task 3 Click, Click
- Modify your program to
- Make the squares clickable. They should change
color to purple when clicked. - Hint Purple 255,0,255
21 if((mouse_xgtshape1_rect(1))...
(mouse_xlt shape1_rect(3))...
(mouse_ygt shape1_rect(2))...
(mouse_ylt shape1_rect(4)))
shape1_color purple end
if((mouse_xgtshape2_rect(1))...
(mouse_xltshape2_rect(3))...
(mouse_ygtshape2_rect(2))...
(mouse_yltshape2_rect(4)))
shape2_color purple end