Matlab Class 2 Xiaotao Su, Ph.D. Visual Psychophysicist - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Matlab Class 2 Xiaotao Su, Ph.D. Visual Psychophysicist

Description:

Matlab Class 2 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) RuCCS * Character Vectors And Numeric Vectors ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 34
Provided by: Robert2469
Category:

less

Transcript and Presenter's Notes

Title: Matlab Class 2 Xiaotao Su, Ph.D. Visual Psychophysicist


1
Matlab Class 2Xiaotao Su, Ph.D.Visual
Psychophysicist IT Group LeaderRutgers Center
for Cognitive Science (RuCCS)
2
Character Vectors And Numeric Vectors
  • 'hello world' is referred to as a character
    string or a character vector
  • 35, 26.3, 11, 4 is a numeric vector
  • These are the two main data types in matlab

h e l l o w o r l d
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
35 26.3 11 4
(1) (2) (3) (4)
3
Character Vectors (aka Strings) And Numeric
Vectors
  • How to convert between these two data types? What
    if I have a string, '39.5' and I want to add to
    it?
  • num2str str2num
  • str2num('39.5') 5
  • 'hello w', 'orld'
  • a 3, 9
  • b 12.6, 8
  • a, b
  • c '1', num2str(39.5)
  • str2num(c) 200

4
Taking inputs
  • Create a new program called math.m
  • math.m
  • Carries out a series of calculations on two
  • numbers
  • written by Your Name July 2010

5
  • clear all
  • take inputs
  • num1input('what is the first number?')
  • num2input('what is the second number?')
  • show calculations
  • disp(num2str(num1), '', num2str(num2), '',
    num2str(num1num2))
  • disp(num2str(num1), '-', num2str(num2), '',
    num2str(num1-num2))

6
boolean expressions
  • Boolean expressions either return 1 for true or
    0 for false
  • Possible operators

gt gt lt lt
equals greater than greater than or equal to less than greater than or equal to and or
7
  • 11
  • (1 1)2
  • (5 5) lt 10
  • (5 5) lt10
  • (1 1) (23)
  • (1 1) (23)
  • 5 (5 lt 10)

Note Put operations inside parenthesis so they
are preformed first.
8
Conditionals (if statements)
  • if(Boolean Expression)
  • do something
  • end
  • you can also do this
  • if(boolean expression)
  • do something
  • else
  • do something else
  • end

or this if(boolean expression) do
something elseif(boolean expr2) do something
else end
9
Back to math.m
  • if ( (round(num1)num1) (round(num2)num2) )
  • disp('num1 and num2 are integers')
  • end

10
  • if ( (round(num1)num1) (round(num2)num2) )
  • disp('num1 and num2 are integers')
  • elseif(round(num1)num1)
  • disp('only num1 is an integer')
  • elseif(round(num2)num2)
  • disp('only num2 is an integer')
  • else
  • disp('neither number is an integer')
  • end

11
Task 1
  • Have math.m tell me which number is larger then
    the other
  • the biggest number is

gt gt lt lt
equals greater than greater than or equal to less than greater than or equal to and or
12
Screen drawing and psychtoolbox
  • Screen Geometry

Origin
Coordinates are measured in
pixels.
X ----------- Positive -gt
Y
Positive
Max X and Y
13
How you typically work with the psychtoolbox
Back Buffer (invisible)
Front Buffer
14
How you typically work with the psychtoolbox
Step 1 Draw Shape to the back buffer
Front Buffer
15
How you typically work with the psychtoolbox
Step 2 Flip the back buffer to the front buffer
16
How you typically work with the psychtoolbox
Back Buffer is automatically cleared
17
How you typically work with the psychtoolbox
Now you can continue with your next frame of
animation
18
How you typically work with the psychtoolbox
Flip the back buffer to the front buffer
19
How you typically work with the psychtoolbox
Back Buffer is automatically cleared
20
Set up program draw_stuff
  • draw_stuff.m
  • Draws stuff on the screen

21
Initialize the main window
  • draw_stuff.m
  • Draws stuff on the screen
  • clear all
  • which_screen0

22
Set up program draw_stuff
  • draw_stuff.m
  • Draws stuff on the screen
  • clear all
  • which_screen0
  • window_ptr, screen_dimensions
  • Screen(0,'OpenWindow', 0, 0, 0)

0
Vector specifying Screen color
Command issued to the psychtoolbox Screen function
0 is main screen (allows for multiple monitors)
red, green, blue
23
Set up program draw_stuff
  • draw_stuff.m
  • Draws stuff on the screen
  • clear all
  • which_screen0
  • window_ptr, screen_dimensionsScreen(0,'OpenWind
    ow', 0, 0, 0)

Vector x1, y1, x2, y2 Which could be 0, 0,
800, 600 For an 800 X 600 display
x1 0 y1 0
x2 800 y2 600
24
Draw shape in back buffer
  • draw_stuff.m
  • Draws stuff on the screen
  • clear all
  • which_screen0
  • window_ptr, screen_dimensionsScreen(0,'OpenWind
    ow', 0, 0, 0)
  • shape_dimensions screen_dimensions/2
  • Screen(window_ptr, 'FillRect', 0,0,255,
    shape_dimensions)

X1, Y1
Front buffer
X2, Y2
Back buffer
25
Flip the back buffer to the front buffer
  • draw_stuff.m
  • Draws stuff on the screen
  • clear all
  • which_screen0
  • window_ptr, screen_dimensionsScreen(0,'OpenWind
    ow', 0, 0, 0)
  • shape_dimensions screen_dimensions/2
  • Screen(window_ptr, 'FillRect', 0,0,255,
    shape_dimensions)
  • Flip the buffers
  • Screen(window_ptr,'Flip')

Front buffer
Back buffer
26
Clear screen
  • ltltltltsnipgtgtgtgt
  • clear all
  • which_screen0
  • window_ptr, screen_dimensionsScreen(0,'OpenWind
    ow', 0, 0, 0)
  • shape_dimensions screen_dimensions/2
  • Screen(window_pointer, 'FillRect', 0,0,255,
    shape_dimensions)
  • Copy to main window
  • Screen(window,'Flip')
  • leave the image up for 5 seconds
  • WaitSecs(5)
  • Clear screen and return control to matlab
  • clear screen

27
Tic and toc
  • tic starts a timer
  • toc tells you the number of seconds since when
    you ran toc

28
While loop
  • while is a type of loop that runs while a
    condition is true
  • while(boolean statement)
  • do something many times
  • end
  • you would stop repeating the loop when the
  • boolean statement is false OR if you use
  • break

29
While program
  • tic
  • loops 0
  • while(toclt5)
  • loops loops1
  • disp('number of loops ', num2str(loops))
  • disp('number of seconds ', num2str(toc))
  • end

30
While program stopping after 10 loops
  • tic
  • loops 0
  • while(toclt5)
  • loops loops1
  • disp('number of loops ', num2str(loops))
  • disp('number of seconds ', num2str(toc))
  • if(loopsgt10)
  • break
  • end
  • end

31
Making a movie
  • draw_stuff.m
  • Draws stuff on the screen
  • clear all
  • which_screen0
  • window_ptr, screen_dimensions
  • Screen(0,'OpenWindow', 0, 0, 0)
  • shape_dimensions screen_dimensions/2
  • Screen(window_pointer, 'FillRect',
  • 0,0,255, shape_dimensions)

32
Task 2
Make the rectangle move to the bottom of the
screen at 5 pixels per frame, then move right
when it hits the bottom. When it hits the right,
the program ends
Hint shape_dimensions(1) is x1, (2) is y1, (3)
is x2, 4 is y2
33
Task 3
When the rectangle hits the bottom make it change
colors to red
Hint color is a 3 number vector red is
255, 0, 0
Write a Comment
User Comments (0)
About PowerShow.com