MATLAB: Can anybody help with persistant errors? (Matlab beginer)

wptr*

Hi all,
I am really new in Matlab and I am programming a small behavioral experiment with images.
Below is the script I am (desperately) trying to run, but keep encountering many errors, specifically 'Undefined function of variable 'wPtr'.
Whenever I evaluate the selection of every section, it seems Ok, but as soon as I try to run the script, errors pop out!
Thank you!
function X
%open the screen
[wPtr,rect]=Screen('OpenWindow',max(Screen('Screens')));
xCenter=rect(3)/2;
yCenter=rect(4)/2;
%Create textures
duckData=imread('C:\Users\louah\Desktop\StimuliEEG_semanticpriming\duck.jpg');
duckTexture=Screen('MakeTexture', wPtr,duckData);
BananaData=imread('C:\Users\louah\Desktop\StimuliEEG_semanticpriming\Banana.jpg');
BananaTexture=Screen('MakeTexture', wPtr,BananaData);
%Get size of image (both images the same size in this example)
%imageHeight
%imageWidth
%colorChannels
[imageHeight, imageWidth,colorChannels]=size(duckData);
%Set up left and right picture locations
gap=100; %distance of pics from center
leftRect=[xCenter-gap-imageWidth, yCenter-imageHeight/2, xCenter+imageHeight/2];
rightRect=[xCenter+gap, yCenter-imageHeight/2, xCenter+gap+imageWidth, yCenter+imageHeight/2];
%set up some vectors with our options
textures=[duckTexture, BananaTexture];
textureNames={'duck', 'Banana'};
rects={leftRect, rightRect};
rectNames={'left', 'right'};
%set up logfile
subjectCode=1;
logfilename=sprintf('%s_log.txt', subjectCode);
logfile=fopen(logfilename, 'a');
%set up response codes
duckCode=KbName('d');
BananaCode=KbName('b');
%hold correctness values
correctnessvalues=[];
%loop for 20 trials
for trial=1:20
%pick a random number 1 or 2
randTextureNum=randi(2);
%now use that number tp pick a texture
ourTexture=textures(randTextureNum);
%draw the pie
%windowPtr=wPtr
Screen('DrawTexture', wPtr, ourTexture, [], ourRect);
stimTime=Screen('Flip', wPtr);
[secs, keyPressed]=KbWait();
Screen('Flip', windowPtr);
response=find(keyPressed);
responseTime=secs-stimTime;
%figure out correctness
correct=0;
if randTextureNum==1
%duck picture was presented
if response==duckCode
correct=1;
end
elseif randTextureNum==2
%Banana picture was presented
if response==BananaCode
correct=1;
end
end
%save correctness
correctnessvalues(end+1)=correct;
%print out to file
fprintf(logfile, '%d\t%s\t%s\t%s\t%.3f\t%d\n', trial, textureNames{randTextureNum},...
rectNames{randRectNum}, KbName(response), responseTime, correct);
WaitSecs(.5);
end

Best Answer

The assignment to wPtr in the first line of the function has been commented out. This means that no value has been given to this variable when it is used in the later call to Screen. So you need to either reinstate that first call to Screen or give wPtr a value some other way.
By the way, your file isn't a script, it's a function. It's worth reading up on the difference in the documentation or a textbook.
Also, note that there is a button for formatting code in questions, and it makes it much easier for people to read your question if you use it.