21
Introduction To Matlab Class 3 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD

Introduction To Matlab Class 3

  • Upload
    franz

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction To Matlab Class 3. Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD. Variables. Integersm = 5;% = [5] Doubles (Floating pt) n = 7.382; Character stringsc1 = ‘beep’ ; % = [‘b’, ‘e’, ‘e’, ‘p’] c2 = ‘4’; - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction To Matlab Class 3

Introduction To MatlabClass 3

Instructors: Hristiyan (Chris) Kourtev

and Xiaotao Su, PhD

Page 2: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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)

Page 3: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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)

== ~= > >= < <= && ||

equals not equal

greater than

greater than or equal to

less than

greater than or equal to

and or

Page 4: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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

Page 5: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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 m m k2 k1 m m m k2 k1 mmm k2 k1 m m m k2 k1 mmm k2

Page 6: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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)

Page 7: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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

Page 8: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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);

Page 9: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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)

Page 10: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

Screen drawing and psychtoolbox

Screen Geometry

X ----------- Positive ->

Y

Positive

Origin

Max X and Y

Coordinates are measured in pixels.

Page 11: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

Basic Display Loop in PsychToolbox (PTB)

% Open Window

% while some condition is true do the following

% draw something

% make some changes

% repeat

% Close screen

Page 12: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

Basic Display Loop in PTB (code)

% draw_stuff.m%% Draws stuff on the screenclear 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);endclear Screen;

catch

clear Screen;

end

Page 13: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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

Page 14: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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

Page 15: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

% mousingAround.m

clear all;

trywhich_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

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

Page 16: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

Skip Sync Tests

• Add to your program before opening the new screen

Screen('Preference','SkipSyncTests', 1);

Page 17: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

Direction/Velocity Vectors

• A vector has both magnitude and direction

• Direction = [x,y]

• Magnitude = v = √(x2+y2) = sqrt(x^2 + y^2) % Pythagorean

x

y xv

Page 18: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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)

Page 19: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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

Page 20: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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];

Page 21: Introduction To Matlab Class 3

Optional Presentation Title

Unit Name

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