Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Class 4 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev.

Similar presentations


Presentation on theme: "Matlab Class 4 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev."— Presentation transcript:

1 Matlab Class 4 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev

2 RuCCS Calling scripts within scripts This is done to modularize code Modular code is useful because you can –reuse the same piece of code in many different programs –have the same piece of code called many times in one program –Only have to debug that piece of code once and then be able to rely on it to work the same way all the time.

3 RuCCS calling scripts % my_prog.m j = 4; double_j if(j<7) double_j else half_j end disp(j) % double_j.m % doubles the value of j j = j*2; % half_j.m % cuts j in half j = j/2;

4 RuCCS calling scripts % my_prog.m j = 4; double_j if(j<7) double_j else half_j end disp(j) % double_j.m % doubles the value of j j = j*2; % half_j.m % cuts j in half j = j/2;

5 RuCCS Added benefit % my_prog.m j = 4; double_j if(j<7) double_j else half_j end disp(j) If double_j and half_j were much more complicated programs the benefit to seperating them out into separate scripts makes our code Shorter Simpler to read Less likely to have bugs You can also have your program perform largely different behaviors based upon different conditions.

6 RuCCS %draw_stuff.m clear all; screen_setup while( …) … screen(window, ‘FillRect’ … flip end clear screen %screen_setup ---Determine Operating System--- if(oldmac|win) Set up screen variables for old mac and windows else Set up screen for OS X end if(osx ==1) Screen(window,'Flip'); else Screen('CopyWindow', window, window_ptr); Screen('CopyWindow', blank, window); Screen(window_ptr, 'WaitBlanking'); end making programs for all versions of psychtoolbox

7 RuCCS Functions Functions are similar to scripts in that –they are separate from your main body of code –used to perform one coherent task –make your code neater Differences –You pass it specific variable(s) and it returns specific variables(s) –The variables within it are not accessible outside the function –The variables outside the function are not accessible inside the function

8 RuCCS %my_prog.m f = 4; k = double_me(f); i = 6; f = double_me(k); disp(f); disp(i); function d_val = double_me(i) %double_me.m %doubles any value passed to it d_val = i*2; Example of functions

9 RuCCS %my_prog.m f = 4; k = double_me(f); i = 6; f = double_me(k); disp(f); disp(i); function d_val = double_me(i) %double_me.m %doubles any value passed to it d_val = i*2; Example of functions Notice, i was set to 6 in my_prog, and i was used in double_me, but the two references didn’t effect eachother. Also each call to a function creates a separate set of variable references for that call. my_prog’s variables f = 4 then 16 k = 8 i = 6 double_me*’s variables i = 4 d_val = 8 double_me**’s variables i = 8 d_val = 16

10 RuCCS %my_prog2.m f = 9; [a, b] = double_times(f, 4); c = double_times(f, 4); disp(a); disp(b); disp(c); function [d_val, t_val] = double_times(i, fact) %double_times.m %doubles any value passed to it and multiplies d_val = i*2; t_val = i*fact; Multiple inputs/outputs

11 RuCCS For loops using different increments for i=1:10 disp(i); end 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 for i=1:2:10 disp(i); end 1, 3, 5, 7, 9 for i=2:2:10 disp(i); end 2, 4, 6, 8, 10for i=10:-2:1 disp(i); end 10, 8, 6, 4, 2 for i=10:-1:1 disp(i); end 10, 9, 8,7,6,5,4,3,2,1

12 RuCCS Task 1 create a function that will take any string and spell it backwards reverse_string(‘stressed’) returns ‘desserts’ for i=1:10 disp(i); end The length of a string is length(str_var) Tips: function d_val = double_me(i)

13 RuCCS clear all; screen_setup shape1_rect = [10, 10, 100, 100]; shape2_rect = shape1_rect + [100, 0, 100, 0]; dot_r = 20; %radius of cursor HideCursor; dot_color = [0, 0, 255]; shape1_color = [255, 0, 0]; shape2_color = shape1_color; tic while(toc<5) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse([window]); cursor = [ mouse_x-dot_r,... mouse_y-dot_r,... mouse_x+dot_r,... mouse_y+dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; end flip; end clear screen; ShowCursor;

14 RuCCS clear all; screen_setup shape1_rect = [10, 10, 100, 100]; shape2_rect = shape1_rect + [100, 0, 100, 0]; dot_r = 20; %radius of cursor HideCursor; dot_color = [0, 0, 255]; shape1_color = [255, 0, 0]; shape2_color = shape1_color; tic while(toc<5) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse cursor = [ mouse_x - dot_r,... mouse_y - dot_r,... mouse_x + dot_r,... mouse_y + dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; end flip; end clear screen; ShowCursor

15 RuCCS %clicking.m clear all; screen_setup shape1_rect = [10, 10, 100, 100]; shape2_rect = shape1_rect + [100, 0, 100, 0]; dot_r = 20; %radius of cursor HideCursor; dot_color = [0, 0, 255]; shape1_color = [255, 0, 0]; shape2_color = shape1_color; tic while(toc<8) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse; cursor = [mouse_x-dot_r,... mouse_y-dot_r,... mouse_x+dot_r,... mouse_y+dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; end flip; end clear screen; ShowCursor;

16 RuCCS Task 2 Make it so every time you click on the rectangles, they change color.

17 RuCCS >>> while(toc<5) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse([window]); cursor = [ mouse_x-dot_r,... mouse_y-dot_r,... mouse_x+dot_r,... mouse_y+dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; if(... (mouse_x>shape1_rect(1))&... (mouse_x< shape1_rect(3))&... (mouse_y< shape1_rect(2))&... (mouse_y< shape1_rect(4))... ) shape1_color = [rand*255, rand*255, rand*255]; end if(... (mouse_x>shape2_rect(1))&... (mouse_x<shape2_rect(3))&... (mouse_y<shape2_rect(2))&... (mouse_y<shape2_rect(4))... ) shape2_color = [rand*255, rand*255, rand*255]; end flip; end clear screen; ShowCursor;

18 RuCCS Task 3 Make the two squares move randomly around the screen bouncing off the walls. They should be still clickable.


Download ppt "Matlab Class 4 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev."

Similar presentations


Ads by Google