Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 5 review fprintf Matrices as images Let's fire up Matlab.

Similar presentations


Presentation on theme: "Day 5 review fprintf Matrices as images Let's fire up Matlab."— Presentation transcript:

1 Day 5 review fprintf Matrices as images Let's fire up Matlab.

2 TODAY Cell and structure arrays Organizing an Experiment
Intro to Psychophysics Toolbox

3 Cell arrays Let's start by loading a data file newexercise_rt.xls
With readtable: data=readtable('newxercise_rt.xls') The table has one column that appears to store strings. Let's extract those into its own array. We can extract one column table: labels=data(:,('trial_label')) Or we can get the stuff “inside the table” with {} Newlabels=data{:,('trial_label')} Newlabels is a “cell array”

4 Cell arrays So, let's try: Newlabels(1) Newlabels{1} Newlabels{1}(1)

5 Cell arrays Newlabels(1) is the cell itself
so trash = Newlabels (1) makes trash a cell Newlabels{1} refers to the value in the cell so trash = Newlabels {1} makes trash a character array Newlabels{1}(j) is the jth element in the character array stored in the cell 1. CHECK OUT THE ARRAY EDITOR… SWEET! (if not already opened, double-click on names, inside your workspace) Analogy to Tables: Newtable=data(:,{'trial_label'}) ( ) means the output is a table, not its contents Newmatrix=data{:,{'Subject','RT'}} {} means the output is the contents of those columns

6 Exercise: compute the average correct RT for each subject

7 Exercise: compute the average correct RT for each subject
for i=1: %%% or max(data.Subject) goodRT= (data.Subject==i & data.error==0); meanRT(i)=mean(data.RT(goodRT)); end; QUESTION: what about subject 3?

8 Structures Multidimensional array elements accessed by textual field designators. Each field can contain any type of Matlab data (numbers, strings, cells, etc). Type: Data.trial = 1; Data.setsize = 3; Data.tgtword = 'doctor'; Data.rt = 541; Data.resp =1; Data

9 Structures Using the "struct" function:
Data = struct('label1', dummy1, 'label2', dummy2, etc); Creates the structure: data.label1 = dummy1 data.label2 = dummy2

10 Structures Initializing a large structure:
Data(64) = struct('label1', dummy1, 'label2', dummy2, etc); Access each element like a vector: Data(34).label1 = 234; Type: data(64) = struct('trial',1,'tid',1,'rt',-1); Q: what's data(34).trial ?

11 Structures Initializing a large structure:
data(n) = struct('field1',value1,'field2',value2) initializes only the nth value. Others are set to empty matrices.

12 Structures Initializing a large structure: Or:
data = repmat(struct('trial',1,'rt',-1),1,64); What's data(11).trial? This way ALL values are initialized with values specified in the struct function.

13 Structures Initializing a large structure: Or:
Use values saved in a cell array. > a = cell(3,1); > a{1} = 'bob'; > a{2} = 'where are you?'; > a{3} = 'I am here'; > data = struct('line',a); What's data(2).line?

14 Structures Retrieving entire fields: > data(1).times =1;
what's data(3).times? > data(3).times = 132; extractimes = [data.times]; % Note: it does not copy what?

15 Structures You can also make arrays within labels if your dummies are themselves arrays. For instance: Imagine you have 2 targets per trial (and two responses) you can create the structure: data(64)= struct('trial',1,'lag',1,'tid', ['X';'A'], 'resp', [0,0]); -elements 1-63 are empty matrices. -elements in location 64 are initialized

16 Structures to access 'X':
data(64)=struct('trial',1,'lag',1,'tid', ['X';'A'], 'resp', [0,0]); to access 'X': data(64).tid(1) %first element in array data(64).tid

17 Structures Why I like structures in experiments?
-Because they're user-friendly for keeping track of what goes where. e.g., data.trial data.condition data.targetloc data.rt data.error

18 Planning an experiment
Your experiment code should be as follows: section 1: SETUP VARIABLES Initialize CONSTANTS (refresh rate) Initialize Variables (with comments so you know what each variable does) Load big files (images, sounds, mex…)

19 Planning an experiment
Section 2: BALANCE CONDITIONS BALANCE your design: -define conditions -how many trials for each condition -Randomize order of trials

20 Planning an experiment
Section 3: TRIAL LOOP 3.1 DRAW constant images (if very complex) 3.2 Start Trial Loop Draw trial specific stimuli Present stimuli 3.2.3 Get response Classify response (error?) 3.2.5 ERASE ANY TRIAL SPECIFIC STIMULI

21 Planning an experiment
Section 4: Save DATA Open a subject file and write data to it. (personal preference). Section 5: CLEAN UP Clear all the variables you used and the images you created, and close any opened files.

22 Planning an experiment
AND DON'T FORGET TO COMMENT AS MUCH AS YOU CAN!!! AND DON'T FORGET SECTION 0: Comments at beginning of file for "help".

23 The Psychophysics Toolbox
A set of functions to: -Interact with Monitor -Interact with Keyboard and mouse -Interact with your OS (Here, Windows)

24 The Psychophysics Toolbox
Today: -Screen basics -keyboard basics

25 The Psychophysics Toolbox
Screen function: -Function that helps us interact with our monitor. - Many "sub-functions". First thing: OpenWindow: [windowPtr,rect]=Screen('OpenWindow',0,[color],[rectangle],[pixelSize],[numberOfBuffers], [stereomode],[multisample],[imagingmode]);

26 OpenWindow OpenWindow:
[windowptr,rect]=Screen('OpenWindow',0,… [color],[rectangle],[pixelSize],...); windowptr: a pointer to the space in memory we are allocating to work on this window (kinda like fid=fopen(..) keeps track of a file) rect: (if specified, and I suggest you do) gives you the coordinates of the window you’ll be using in pixels, on the format [Xtop-left, Ytop-left, Xbotton-right, Ybottom-right]. e.g. [ ] default is whole Screen. (best to use default)

27 OpenWindow Parenthesis:
ALL YOU NEED TO DEFINE A RECTANGLE is 2 Points: TOPLEFT AND BOTTOM RIGHT CORNERS top Left Bottom right

28 OpenWindow [window,rect]=Screen('OpenWindow',0,[color], [rect],[pixelSize]); 0: refers to the main monitor (where you'll be presenting stimuli).Change for multiple monitors color: you want the window to be: if one number: an index (CLUT, between 0-255), or a RGB triplet [r g b]. Later we'll talk about changing the CLUT. pixelsize: you can set the pixelsize for your Screens (8 bit -> 256 colors, 24 bit…). Default is unchanged (determined by your computer hardware). TAKES ABOUT 1-3 Seconds.

29 Close Window Two ways: To close all windows: Screen('CloseAll');
(or sca) To close a specific window: Screen('Close’,windowPtr); VERY IMPORTANT!!!

30 Flip When you draw, write or import an image to Matlab, it will be put in the Buffer in your graphics card memory. To copy the image to the Display, you use Flip. Screen(‘Flip’,windowPtr,[when],[dontclear], [dontsync],[multiflip]); when: specify when, as “in 2 seconds from current clock time” (use GetSecs, see later)

31 Flip Screen(‘Flip’,windowPtr,[when],[dontclear], [dontsync],[multiflip]); dontclear: 0 (default): it erases the buffer (so you can draw anew in it). 1: Leaves the current contents of the buffer untouched. Next drawing command Adds to contents. 2: Switches contents of Display and Buffer.

32 Flip Screen(‘Flip’,windowPtr,[when],[dontclear], [dontsync],[multiflip]); dontsync: 0: default. Puts Matlab to sleep while it waits for the time to “Flip”. (can’t check keyboard or mouse!) 1: Allows for Matlab to do other stuff, while it waits for the Flip time.(TIME STAMPS ARE NO LONGER ACCURATE!! See next)

33 Flip Optional outputs:
[VBLTimeStamp StimulusOnsetTime FlipTimeStamp Missed Beamposition]=Screen(‘Flip’,...); VBLTimeStamp: system time when the flip has happened. StimulusOnsetTime: estimate. Beamposition: where the beam waswhen the time measurement was taken. FlipTimeStamp: time at end of Flip Execution. Flip execution time=FlipTimeStamp-VBLTimeStamp

34 Write on the Screen. Exercise: Using the guide, write a script that:
1. Opens a white window 2. Write "Hello World" in it 3. Waits for user to push key 4. Close the window Screen('DrawText', windowptr, string,x,y,color); KbWait;

35 Write on the Screen. First line in your code should be:
warning off MATLAB:DeprecatedLogicalAPI Screen('Preference', 'SkipSyncTests', 1); [window,rect]=Screen('OpenWindow',0,255); Screen('DrawText',window,'Hello World',100,100,[ ]); Screen('Flip', window); KbWait; Screen('CloseAll');

36 Why KbWait? KbWait: returns the time (number of seconds since you turned on the computer). It does NOT push forward the command window (leaves it in the back)

37 Write on the Screen. Add the following: -Make the background black
-Write Hello World in RED, Font 40 (pixels tall), in 'Times New Roman'

38 Write on the Screen. warning off MATLAB:DeprecatedLogicalAPI
[window,rect]=Screen('OpenWindow',0,0); Screen('TextFont',window,'Times New Roman'); Screen('TextSize',window,40); Screen('DrawText',window,'Hello World', ,100,[ ]); Screen('Flip',window); KbWait; Screen('CloseAll');

39 Write on the Screen. DETERMINE HOW LONG IT TOOK MATLAB TO OPEN THE WINDOW AND WRITE TEXT TO IT. Use: GetSecs (returns seconds since the computer was last turned on);

40 Write on the Screen. warning off MATLAB:DeprecatedLogicalAPI
t1=GetSecs; [window,rect]=Screen('OpenWindow',0,0); Screen('TextFont',window,'Times New Roman'); Screen('TextSize',window,40); Screen('DrawText',window,'Hello World', ,100,[ ]); Screen('Flip',window); t2=GetSecs; KbWait; Screen('CloseAll'); time2open=round((t2-t1)*1000); fprintf('It took %d milliseconds to do so', time2open);

41 Write on the Screen. HOW LONG? -a few seconds to open Screen!
That's why WE NEED to do that once and keep the “buffer” open, until we’re all done. Two strategies: If changes are small-to-large, we can write to the buffer quickly enough. If changes are large, use “textures” (separate buffers you can draw on ahead of time).

42 How long does it take to switch between two buffers?
Change code to first write Hello, then immediately write and present the word “World”. Measure how long it takes.

43 How long does it take to switch between two buffers?
[newX, newY]=Screen('DrawText',window,'Hello ‘, 100,100,[ ]); Screen('Flip',window,[], 1);%don’t erase Hello! t1=GetSecs; Screen('DrawText',window,'World ',newX,newY,... [ ]); Screen('Flip',window); t2=GetSecs;

44 How long does it take to switch between two buffers?
Answer: One refresh. Stimuli sync and everything Ok! If you ever get an answer larger than one refresh, time to use Textures... (later)

45 SLACK Flip can be scheduled in the future. But there will always be a “slack time” in the scheduling due to your operating system. So schedule “ahead of time”! use: slack=Screen(‘GetFlipInterval’,window)/2; Try presenting “World” exactly 500 ms after Hello.

46 SLACK How long did it take?
slack=Screen('GetFlipInterval',window)/2; Screen('TextFont',window,'Times New Roman'); Screen('TextSize',window,40); [newX, newY]=Screen('DrawText',window,'Hello ',100,100,[255,0, 0]); vbl=Screen('Flip',window,[], 1); t1=GetSecs; Screen('DrawText',window,'World ',newX,newY,[ ]); Screen('Flip',window, t slack); t2=GetSecs; KbWait; How long did it take? Try it several times (with and without slack).

47 MakeTexture We can use MakeTexture to transform a matrix (hint: all jpg, gifs, etc... are matrices!) into a “texture” that can be drawn to the buffer. textureIndex=Screen(‘MakeTexture’,WindowIndex,imageMatrix); imageMatrix can be luminance (2D matrix), RGB values (3D matrix) or even include alpha values RGBA (transparency, 4D matrix... need to turn on Screen(‘BlendFunction’))

48 DrawTexture So, Once we have our texture, we THEN Draw it to our buffer. Screen(‘DrawTexture’,windowPointer,TextureIndex, [sourceRect],[destinationRect],[rotationAngle],[, filterMode] [, globalAlpha] [, modulateColor] [, textureShader] [, specialFlags] [, auxParameters]); SourceRect: Part of Texture to be drawn (default is whole texture) DestinationRect: subpart of display window where it will be drawn (default is centered on Screen). RotationAngle: Rotation angle in degrees! Default=0 = upright!

49 DrawTexture if [srcRect] and [dstRect] do not have same size: filterMode establishes algorythm to shrink/enlarge texture. source window hello hello Destination window

50 DrawTexture So, Once we have our texture, we THEN Draw it to our buffer. Screen(‘DrawTexture’,windowPointer,TextureIndex, [sourceRect],[destinationRect],[rotationAngle],[, filterMode] [, globalAlpha] [, modulateColor]); globalAlpha: Range is 0 (fully transparent) to 1 (fully opaque) for blending texture with image in buffer. modulateColor = multiplies R G B values for the whole texture. So, [ ] does: Red: diminished intensity by 50% (128/255) Green: Nothing (255/255) Blue: No blue intensity! (0!) Nice for coloring weird shapes defined as textures!

51 Exercise Draw Durer to the display. Question, how long did it take from MakeTexture to presenting it?

52 Exercise Draw Durer to the display. Question, how long did it take from MakeTexture to presenting it? warning off MATLAB:DeprecatedLogicalAPI [window,rect]=Screen('OpenWindow',2,0); load durer; t1=GetSecs; durer=Screen('MakeTexture',window,X); Screen('DrawTexture',window,durer); Screen('Flip',window); t2=GetSecs; KbWait; Screen('CloseAll'); time2open=round((t2-t1)*1000); fprintf('It took %d milliseconds to do so',time2open);

53 Exercise Draw Durer to the display. Question, how long did it take from MakeTexture to presenting it? It took more than one refresh! So, make these type of textures ahead of time!!

54 Exercise Make a loop that makes Durer rotate in 10 degree increments 36 times, waiting 3 refreshes between flips. [window,rect]=Screen('OpenWindow',2,0); refresh=Screen('GetFlipInterval',window); load durer; durer=Screen('MakeTexture',window,X); vbl=Screen('Flip',window); %synchronizing to retrace. for i=1:36 Screen('DrawTexture',window,durer,[],[],10*i); vbl=Screen('Flip',window , vbl + (3-0.5)*refresh,[],1); end; KbWait; Screen('CloseAll');

55 Drawing with the Toolbox
NICE: We can draw circles, squares, etc with intuitive commands: DrawLine, FillPoly, FillRect… You can explore those on your own. But, here's the key for ovals, circles etc…

56 Drawing with the Toolbox
x y x y x y+50 x y+60 Ovals are circumscribed in rectangles. Here rect = [x y x y+60], where xsize = 100 and ysize =60 Circles are circumscribed in squares, meaning xsize = ysize. Arcs are drawn within the rectangle that would have circumscribed the entire oval/circle the arc belongs to. x y HELLO WORLD! TextSize

57 Exercise slow_RSVP Write a program that:
Asks the user for his/her name. b. Waits 2 second. c. Writes the user name at fixation, one letter at a time, at a speed of about 1000 ms per letter. d. waits for user keypress before exiting.


Download ppt "Day 5 review fprintf Matrices as images Let's fire up Matlab."

Similar presentations


Ads by Google