# Check if Texture Exists

**URL:** <https://psychtoolbox.discourse.group/t/check-if-texture-exists/3991>\
**Category:** Programming Help\
**Created:** [August 27, 2021, 9:02am UTC](https://psychtoolbox.discourse.group/t/check-if-texture-exists/3991 "2021-08-27T09:02:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsolomyak](https://avatars.discourse-cdn.com/v4/letter/l/85f322/32.png) [@lsolomyak](https://psychtoolbox.discourse.group/u/lsolomyak)\
**Post date:** [August 27, 2021, 9:02am UTC](https://psychtoolbox.discourse.group/t/check-if-texture-exists/3991/1 "2021-08-27T09:02:31Z")

</div>

Is there a function to check whether a texture or window already exists or whether I need to create one? Furthermore, if I do have an error i MakeTexture is there a way of catching the error before closing the screen?

I keep getting the error that no window or texture exists even though i have a window open and a texture pre-created. This error closes my window.

Error in function DrawTextures: Invalid Window (or Texture) Index provided: It doesn’t correspond to an open window or texture.

My code is

```auto
   % Make the image into a texture
        imageTexture1=params.range_ar_trials.texture(params.range_ar_trials.numbers==double(data.trialdata.stim1(trial)))
         imageTexture2=params.range_ar_trials.texture(params.range_ar_trials.numbers==double(data.trialdata.stim2(trial)))

   try 
        Screen('DrawTextures', window,imageTexture1, [], params.left_rect);
        Screen('DrawTextures', window,imageTexture2, [], params.right_rect);
        catch
         left_stimuli=stimuli.image(find(left_stim==stimuli.number));
        right_stimuli=stimuli.image(find(right_stim==stimuli.number));
        
        imag1=stim(left_stimuli).image;
        imag2=stim(right_stimuli).image;
         imageTexture1 = Screen('MakeTexture', window,imag1 );
         imageTexture2 = Screen('MakeTexture', window,imag2 );
        Screen('DrawTextures', window,imageTexture1, [], params.left_rect);
        Screen('DrawTextures', window,imageTexture2, [], params.right_rect);
        end

```

And in my workspace I have

```auto
window: 10
imageTexture1: 37
params.left_rect

ans =

   84.8000
  184.8000
  315.2000
  415.2000

```

Any help would be greatly appreciated
