210 likes | 357 Views
Introduction To Matlab Class 3. Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD. Variables. Integers m = 5; % = [5] Doubles (Floating pt) n = 7.382; Character strings c1 = ‘beep’ ; % = [‘b’, ‘e’, ‘e’, ‘p’] c2 = ‘4’;
E N D
Introduction To MatlabClass 3 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD
Variables • 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)
Boolean Expressions • Boolean operands • 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 firste.g. 0 || (4<5)
Loops (for and while) • For loopfor index = from:to % do somethingend • While loopwhile(condition) % do something % change something that affects value of “condition”end
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 %nested loop example for k = 1:max_loops disp(‘k1’); for m = 1:3 disp(‘m’); end disp(‘k2’); end % outputs: % k1 m mm k2 k1 m mm k2 k1 mmm k2 k1 m mm k2 k1 mmm k2
Commonly 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(rand*20)
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 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
Multiple 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);
Collecting User Input & Using it • Take input from keyboardnum1=input('what is the first number?'); • Validation checks: - isstr(var)- isnum(var) • Converting from strings to numbers and back- num2str(var)- str2num(var)
Screen drawing and psychtoolbox Screen Geometry Origin Coordinates are measured in pixels. X ----------- Positive -> Y Positive Max X and Y
Basic Display Loop in PsychToolbox (PTB) % Open Window % while some condition is true do the following % draw something % make some changes % repeat % Close screen
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; 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
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 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
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
while(toc<5) • [mouse_x, mouse_y, buttons] = GetMouse; • if(sum(buttons)>0) • dot_color = red; • else • dot_color = green; • end • cursor = [ mouse_x-dot_r, mouse_y-dot_r, ... • mouse_x+dot_r, mouse_y+dot_r]; • Screen(window_ptr, 'FillOval', dot_color, cursor); • Screen('Flip', window_ptr); • end • clear Screen; • ShowCursor; • catch • clear Screen; • ShowCursor; • end % mousingAround.m clear all; try which_screen=0; 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
Skip Sync Tests • Add to your program before opening the new screenScreen('Preference','SkipSyncTests', 1);
Direction/Velocity Vectors • A vector has both magnitude and direction • Direction = [x,y] • Magnitude = v = √(x2+y2) = sqrt(x^2 + y^2) % Pythagorean x v y x
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)
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
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];
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