1 / 57

Day 5 review

Day 5 review. fprintf Matrices as images Let's fire up Matlab. TODAY. Cell and structure arrays Organizing an Experiment Intro to Psychophysics Toolbox. Cell arrays. Let's start by loading a data file newexercise_rt.xls With readtable : data= readtable ('newxercise_rt.xls')

mbeus
Télécharger la présentation

Day 5 review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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:20 %%% 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; > data(2).times =3; 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 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.trialdata.conditiondata.targetlocdata.rtdata.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 Loop3.2.1 Draw trial specific stimuli 3.2.2 Present stimuli 3.2.3 Get response 3.2.4 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. [0 0 1280 1024] 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,[0 255 255 255]); 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,100,[ 255 0 0]); 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,100,[ 255 0 0]); 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,[ 255 0 0]); Screen('Flip',window,[], 1);%don’t erase Hello! t1=GetSecs; Screen('DrawText',window,'World ',newX,newY,...[ 255 0 0]); Screen('Flip',window); t2=GetSecs;

  44. How long does it take to switch between two buffers? How long? 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 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,[ 255 0 0]); Screen('Flip',window, t1 + 0.500 - 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 source window if [srcRect] and [dstRect] do not have same size: filterMode establishes algorythm to shrink/enlarge texture. 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, [128 255 0] 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!

More Related