Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction To Matlab Class 5 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD."— Presentation transcript:

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

2 Optional Presentation Title Unit Name Accurate Timing GetSecs is a psychtoolbox function that provides highly accurate timing. It returns the number of seconds since the machine has started up To test precision on your machine, type: GetSecsTick a = GetSecs GetSecs – a Command Window:

3 Optional Presentation Title Unit Name KbCheck KbCheck is used to check if a key is being pressed and to find out what key is being pressed. Returns three values [key_press, secs, key_code]=KbCheck

4 Optional Presentation Title Unit Name KbCheck KbCheck is used to check if a key is being pressed and to find out what key is being pressed. Returns three values [key_press, secs, key_code]=KbCheck 1 or 0 depending on if there was a key press Time that this test took place (This is from GetSecs function) Vector representing all of the input keys. Elements in the vector are set to 1 if pressed or 0 if not

5 Optional Presentation Title Unit Name Speed Test Game %speed_test.m %tests your reaction time WaitSecs(1); disp('Ready?'); num_sec_to_start = rand*5+1; WaitSecs(num_sec_to_start); too_soon = 0; timer = GetSecs; while ((GetSecs-timer)<num_sec_to_start)... && (~too_soon) [too_soon, secs, keyCode] = KbCheck; if(too_soon) break; end if(too_soon) disp('you pressed the key too early'); else disp('NOW!'); timer = GetSecs; while 1 [key_is_down, secs, key_code] = KbCheck; if(key_is_down) break end elapsed_time = secs - timer; disp(['Your time was ', num2str(elapsed_time)]); end

6 Optional Presentation Title Unit Name Task 1 Make it so if someone presses a key too early, the program give a message and exits. [key_press, secs, key_code]=KbCheck 1 or 0 depending on if there was a key press Time that this test took place (This is from GetSecs function) Vector representing all of the input keys. Elements in the vector are set to 1 if pressed or 0 if not (Tip:first remove WaitSecs

7 Optional Presentation Title Unit Name Speed Test Game %speed_test.m %tests your reaction time WaitSecs(1); disp('Ready?'); num_sec_to_start = rand*5+1; WaitSecs(num_sec_to_start); too_soon = 0; timer = GetSecs; while ((GetSecs-timer)<num_sec_to_start)... && (~too_soon) [too_soon, secs, keyCode] = KbCheck; if(too_soon) break; end if(too_soon) disp('you pressed the key too early'); else disp('NOW!'); timer = GetSecs; while 1 [key_is_down, secs, key_code] = KbCheck; if(key_is_down) break end elapsed_time = secs - timer; disp(['Your time was ', num2str(elapsed_time)]); end

8 Optional Presentation Title Unit Name What key was pressed? The third returned value is a vector representing all the keys with 1’s for pressed keys and zeros for keys that aren’t pressed To find the value of the key, use: key_name = KbName(key_code); This will return a string corresponding to that key_code

9 Optional Presentation Title Unit Name Simple program to find key mapping names % get_key_name.m %find out what the name of the key is WaitSecs(1); KbWait; [key_is_down, secs, key_code] = KbCheck; name = KbName(key_code); disp(name);

10 Optional Presentation Title Unit Name KbName works the other way around as well As you have seen: key_name = KbName(key_code); You can also do the following: return_key = KbName(‘return’); esc_key = KbName(‘esc’); up_key = KbName(‘up’); And this will get you the index of the element in the key_code vector that corresponds to that key

11 Optional Presentation Title Unit Name key_code vector when esc is pressed 00 ….00100….. (1)(2) ….(25)(26)(27)(28)(29)…. key_code vector: Values: Indexes So, key_code(27) is 1 esc’s key code is 27

12 Optional Presentation Title Unit Name Exit on esc %exit_on_esc.m esc_key = KbName('esc'); i = 0; while 1 i = i+1 [key_is_down, secs, key_code] = KbCheck; if(key_code(esc_key)) break; end disp('Good Bye');

13 Optional Presentation Title Unit Name Moving Dot %moving_dot.m clear all; screen_setup; up_key = KbName('up'); down_key = KbName('down'); left_key = KbName('left'); right_key = KbName('right'); esc_key = KbName('esc'); shape = [20, 20, 100, 100]; color = [255, 0, 255]; tic while toc<10 Screen(window, 'DrawOval', … color, shape); flip; [key_is_down, secs, key_code] … = KbCheck; if(key_code(esc_key)) break end clear screen;

14 Optional Presentation Title Unit Name Task 2 Make it so the arrow keys will move the dot


Download ppt "Introduction To Matlab Class 5 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD."

Similar presentations


Ads by Google