Matlab Class 2 - Rutgers University
Download
Report
Transcript Matlab Class 2 - Rutgers University
Introduction To Matlab
Class 3
Instructors: Hristiyan (Chris) Kourtev
and Xiaotao Su, PhD
Optional Presentation Title
Variables
• Integers
• Doubles (Floating pt)
• Character strings
•
•
•
•
•
•
m = 5; % = [5]
n = 7.382;
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)
Unit Name
Optional Presentation Title
Boolean Expressions
• Boolean operands
==
~=
>
>=
equals not
greater greater than
equal than
or equal to
<
less
than
<=
greater than
or equal to
&& ||
and or
• Boolean expressions either return
1 for true e.g. 5 == 5
or
0 for false e.g. 5 > 9
• Put expressions in parentheses so they get evaluated first
e.g.
0 || (4<5)
Unit Name
Optional Presentation Title
Loops (for and while)
• For loop
for index = from:to
% do something
end
• While loop
while(condition)
% do something
% change something that affects value of “condition”
end
Unit Name
Optional Presentation Title
Loops (for and while) -- examples
max_loops = 5;
for index = 1:max_loops
disp(index);
end
counter = 1;
while(counter < max_loops)
disp(counter);
counter = counter + 1;
end
Unit Name
%nested loop example
for k = 1:max_loops
disp(‘k1’);
for m = 1:3
disp(‘m’);
end
disp(‘k2’);
end
% outputs:
% k1 m m m k2 k1 m m m k2 k1
mmm k2 k1 m m m k2 k1
mmm k2
Optional Presentation Title
Commonly used functions
• rand - generates a random decimal number between 0 and 1
e.g. 0.3456 or 0.9993 or 0.0013 etc
• ceil(num) – returns the next integer bigger than the input
e.g. ceil(5.56) 6 or ceil(2.1) 3 or ceil(6) 6
• floor(num) – returns the next integer, smaller than the input
e.g. floor(0.9) 0 or floor(-0.1) -1
• To generate a random number between 0 & 20:
ceil(rand*20)
Unit Name
Optional Presentation Title
Commonly 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 matrix
e.g. length(m) 4, length(n) 4, lenth(k) 3
• size(mat,dim) – returns all the dimensions of a matrix/vector
e.g. size(m) [1, 4], size(n) [2, 4],
size(k) [3, 1], size (n, 2) 4
Unit Name
Optional Presentation Title
Multiple Input/Output Functions
• Functions can have more than one input and more than one
output
e.g. s = size(mat, dim);
• Storing returned values in 2 or more separate variables
e.g. [x, y] = size(mat);
• Storing returned values in a vector/cell array
e.g. vals = size(mat);
Unit Name
Optional Presentation Title
Collecting User Input & Using it
• Take input from keyboard
num1=input('what is the first number?');
• Validation checks:
- isstr(var)
- isnum(var)
• Converting from strings to numbers and back
- num2str(var)
- str2num(var)
Unit Name
Optional Presentation Title
Screen drawing and psychtoolbox
Origin
Screen Geometry
X ----------- Positive ->
Y
Positive
Unit Name
Coordinates are
measured in
pixels.
Max X and Y
Optional Presentation Title
Basic Display Loop in PsychToolbox (PTB)
% Open Window
% while some condition is true do the following
% draw something
% make some changes
% repeat
% Close screen
Unit Name
Optional Presentation Title
Basic Display Loop in PTB (code)
% draw_stuff.m
%
% Draws stuff on the screen
clear all;
try
which_screen=0;
bg_color = [0, 0, 0];
[window_ptr, screen_dimensions]= …
Screen(which_screen, …
'OpenWindow',bg_color);
shape_dimensions = …
screen_dimensions/2;
tic;
Unit Name
while (toc < 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
clear Screen;
catch
clear Screen;
end
Optional Presentation Title
Moving 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 5
e.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
Unit Name
Optional Presentation Title
Task 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 mouse
[mouse_x, mouse_y, buttons] = GetMouse(window_ptr);
• Hide and show the windows cursor:
HideCursor, ShowCursor
• Draw a circle:
Screen(window, 'FillOval', dot_color, cursor_dim);
• If sum(buttons)>0, a button has been clicked
Unit Name
Optional Presentation Title
% mousingAround.m
while(toc<5)
[mouse_x, mouse_y, buttons] = GetMouse;
clear all;
if(sum(buttons)>0)
try
dot_color = red;
which_screen=0;
else
bg_color = [0, 0, 0];
dot_color = green;
[window_ptr,
end
screen_dimensions]= …
cursor = [ mouse_x-dot_r, mouse_y-dot_r, ...
Screen(which_screen, …
mouse_x+dot_r, mouse_y+dot_r];
'OpenWindow', bg_color);
Screen(window_ptr, 'FillOval', dot_color, cursor);
Screen('Flip', window_ptr);
dot_r = 20; %radius of cursor
end
HideCursor;
clear Screen;
green = [0, 255, 0];
ShowCursor;
red = [255,0, 0];
catch
clear Screen;
tic
ShowCursor;
end
Unit Name
Optional Presentation Title
Skip Sync Tests
• Add to your program before opening the new screen
Screen('Preference','SkipSyncTests', 1);
Unit Name
Optional Presentation Title
Direction/Velocity Vectors
• A vector has both magnitude and direction
x
y
v
x
• Direction = [x,y]
• Magnitude = v = √(x2+y2) = sqrt(x^2 + y^2) % Pythagorean
Unit Name
Optional Presentation Title
Detecting 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) <= screen_dimensions(2)
• Collision w/ left border if: rect1(1) <= screen_dimensions(1)
• Collision w/ bottom border if: rect1(4) >= screen_dimensions(4)
• Collision w/ right border if: rect1(3) >= screen_dimensions(2)
Unit Name
Optional Presentation Title
Task 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
Unit Name
Optional Presentation Title
Task 3 – Click, Click
Modify your program to:
• Make the squares clickable. They should change color to
purple when clicked.
Hint: Purple = [255,0,255];
Unit Name
Optional Presentation Title
if((mouse_x>shape1_rect(1))&...
(mouse_x< shape1_rect(3))&...
(mouse_y> shape1_rect(2))&...
(mouse_y< shape1_rect(4)))
shape1_color = purple;
end
if((mouse_x>shape2_rect(1))&...
(mouse_x<shape2_rect(3))&...
(mouse_y>shape2_rect(2))&...
(mouse_y<shape2_rect(4)))
shape2_color = purple;
end
Unit Name