# Error using DrawTexture

**URL:** <https://psychtoolbox.discourse.group/t/error-using-drawtexture/297>\
**Category:** Uncategorized\
**Created:** [August 16, 2013, 7:13pm UTC](https://psychtoolbox.discourse.group/t/error-using-drawtexture/297 "2013-08-16T19:13:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [August 16, 2013, 7:13pm UTC](https://psychtoolbox.discourse.group/t/error-using-drawtexture/297/1 "2013-08-16T19:13:57Z")

</div>

Hi,  
  
I'm having trouble with a script when trying to use DrawTexture.  
  
I keep getting the error message below:  
  
displaycommand = Screen('DrawTexture',w,imagetex(i),[],pRect{i});  
  
Error in function DrawTexture: Invalid Window (or Texture) Index provided: It doesn't correspond to an open window or texture.  
Did you close it accidentally via Screen('Close') or Screen('CloseAll') ?  
??? Error using ==\> Screen  
Usage:  
  
Screen('DrawTexture', windowPointer, texturePointer [,sourceRect] [,destinationRect] [,rotationAngle] [,  
filterMode] [, globalAlpha] [, modulateColor] [, textureShader] [, specialFlags] [, auxParameters]);  
  
Error in ==\> MSS\_test at 654  
 eval(displaycommand);  
  
Nowhere in the script is there a Screen('Close').   
  
Here is the script I think the error message is referring to :  
  
% setup text to be displayed (as written, will center text and  
 % wrap at default\_wrap pixels)  
 displaycommand = sprintf('DrawFormattedText\_new(w,stFile{i},''center'', ''center'',black, default\_wrap,hshift(i),vshift(i));');  
  
 elseif st(i) == 's' %primary stimulus is sound  
 eval(bg\_display); %present the background file while sound is playing  
 Screen('Flip',w);  
  
 t1 = GetSecs;  
  
 % Open the default audio device [], with default mode [] (==Only playback),  
 % and a required latencyclass of zero 0 == no low-latency mode, as well as  
 % a frequency of stSoundList{i,2} = fs, and nrchannels sound channels.  
 % This returns a handle to the audio device:  
 pahandle = PsychPortAudio('Open', [], [], 1, stSoundList{i,2},nrchannels);  
  
 % Fill the audio playback buffer with the audio data 'stSoundList{i,1} = x from above':  
 PsychPortAudio('FillBuffer', pahandle, stSoundList{i,1}');  
 t2 = GetSecs;  
  
 if DEBUG  
 fprintf( 'It took %f seconds to open PsychPortAudio and fill the buffer in iteration %d\n',t2-t1,i);  
 end  
  
 % Start audio playback for 'repetitions' repetitions of the sound data,  
 % start it immediately (0) and wait for the playback to start, return onset  
 % timestamp.  
 t1 = PsychPortAudio('Start', pahandle, rep(i), 0, 1);  
 onset(i) = t1 - experiment\_start\_time; %sound onset  
 sound\_closed = 0;  
  
 displaycommand = bg\_display; %For sound, foreground and background images are same  
  
 elseif st(i) == 'p' %primary stimulus is a picture  
 displaycommand = sprintf('Screen(''DrawTexture'',w,imagetex(i),[],pRect{i});');  
 end; % if st(i)  
  
 if DEBUG  
 fprintf('\ni= %d,\nst(i) = %c\n',i, st(i));  
 fprintf('displaycommand = %s\n\n',displaycommand);  
 end;  
  
  
 %%%%%%%%%%%%%%%%%%%%%%%  
 %Put stimuli on screen  
 %%%%%%%%%%%%%%%%%%%%%%%  
  
 t1 = GetSecs;  
 eval(displaycommand);  
 [vbltime,start\_time]=Screen('Flip',w);  
 t2 = GetSecs;  
 fprintf('We needed %f seconds to Draw texture %d.\n',t2-t1,i)  
  
  
Also, I am using a Windows 7 machine with Matlab 2009b in 32-bit and Psychtoolbox 3.   
  
Any help is appreciated.  
  
Sonia

---

<div class="post-metadata">

**Author:** ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)\
**Post date:** [August 16, 2013, 10:30pm UTC](https://psychtoolbox.discourse.group/t/error-using-drawtexture/297/2 "2013-08-16T22:30:52Z")

</div>

you need to use Screen('MakeTexture') to create the texture before drawing it. See Screen'MakeTexture?') for help

  
  

> On Sat, Aug 17, 2013 at 3:13 AM, soniakrol \<[soniakrol@...](mailto:soniakrol@...)\> wrote:  
> 
> > Hi,  
> >   
> > I'm having trouble with a script when trying to use DrawTexture.  
> >   
> > I keep getting the error message below:  
> >   
> > displaycommand = Screen('DrawTexture',w,imagetex(i),[],pRect{i});  
> >   
> > Error in function DrawTexture: Invalid Window (or Texture) Index provided: It doesn't correspond to an open window or texture.  
> > Did you close it accidentally via Screen('Close') or Screen('CloseAll') ?  
> > ??? Error using ==\> Screen  
> > Usage:  
> >   
> > Screen('DrawTexture', windowPointer, texturePointer [,sourceRect] [,destinationRect] [,rotationAngle] [,  
> > filterMode] [, globalAlpha] [, modulateColor] [, textureShader] [, specialFlags] [, auxParameters]);  
> >   
> > Error in ==\> MSS\_test at 654  
> > eval(displaycommand);  
> >   
> > Nowhere in the script is there a Screen('Close').   
> >   
> > Here is the script I think the error message is referring to :  
> >   
> > % setup text to be displayed (as written, will center text and  
> > % wrap at default\_wrap pixels)  
> > displaycommand = sprintf('DrawFormattedText\_new(w,stFile{i},''center'', ''center'',black, default\_wrap,hshift(i),vshift(i));');  
> >   
> > elseif st(i) == 's' %primary stimulus is sound  
> > eval(bg\_display); %present the background file while sound is playing  
> > Screen('Flip',w);  
> >   
> > t1 = GetSecs;  
> >   
> > % Open the default audio device [], with default mode [] (==Only playback),  
> > % and a required latencyclass of zero 0 == no low-latency mode, as well as  
> > % a frequency of stSoundList{i,2} = fs, and nrchannels sound channels.  
> > % This returns a handle to the audio device:  
> > pahandle = PsychPortAudio('Open', [], [], 1, stSoundList{i,2},nrchannels);  
> >   
> > % Fill the audio playback buffer with the audio data 'stSoundList{i,1} = x from above':  
> > PsychPortAudio('FillBuffer', pahandle, stSoundList{i,1}');  
> > t2 = GetSecs;  
> >   
> > if DEBUG  
> > fprintf( 'It took %f seconds to open PsychPortAudio and fill the buffer in iteration %d\n',t2-t1,i);  
> > end  
> >   
> > % Start audio playback for 'repetitions' repetitions of the sound data,  
> > % start it immediately (0) and wait for the playback to start, return onset  
> > % timestamp.  
> > t1 = PsychPortAudio('Start', pahandle, rep(i), 0, 1);  
> > onset(i) = t1 - experiment\_start\_time; %sound onset  
> > sound\_closed = 0;  
> >   
> > displaycommand = bg\_display; %For sound, foreground and background images are same  
> >   
> > elseif st(i) == 'p' %primary stimulus is a picture  
> > displaycommand = sprintf('Screen(''DrawTexture'',w,imagetex(i),[],pRect{i});');  
> > end; % if st(i)  
> >   
> > if DEBUG  
> > fprintf('\ni= %d,\nst(i) = %c\n',i, st(i));  
> > fprintf('displaycommand = %s\n\n',displaycommand);  
> > end;  
> >   
> > %%%%%%%%%%%%%%%%%%%%%%%  
> > %Put stimuli on screen  
> > %%%%%%%%%%%%%%%%%%%%%%%  
> >   
> > t1 = GetSecs;  
> > eval(displaycommand);  
> > [vbltime,start\_time]=Screen('Flip',w);  
> > t2 = GetSecs;  
> > fprintf('We needed %f seconds to Draw texture %d.\n',t2-t1,i)  
> >   
> > Also, I am using a Windows 7 machine with Matlab 2009b in 32-bit and Psychtoolbox 3.   
> >   
> > Any help is appreciated.  
> >   
> > Sonia
