# Cannot terminate the screen with a button

**URL:** <https://psychtoolbox.discourse.group/t/cannot-terminate-the-screen-with-a-button/29>\
**Category:** Uncategorized\
**Created:** [May 23, 2019, 4:14am UTC](https://psychtoolbox.discourse.group/t/cannot-terminate-the-screen-with-a-button/29 "2019-05-23T04:14:43Z")\
**Posts on this page:** 3\
**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:** [May 23, 2019, 4:14am UTC](https://psychtoolbox.discourse.group/t/cannot-terminate-the-screen-with-a-button/29/1 "2019-05-23T04:14:43Z")

</div>

Hi,

I am trying to learn MATLAB on my own and has struggled through with my mac airbook, I have switched to Linux for it and it works fine except one thing that I cannot terminate screen with any button after I opened it. And Idk why. I could terminate it with sca code but I do not want to do it for purposes of next codes. I also try ErrorCathDemo and it show like this;

This is the code I am trying. After it opens grey screen, I am not able to close it :(&nbsp;   

```
% Clear the workspace and the screensca;
closeall;
clearvars;% Here we call some default settings for setting up PsychtoolboxPsychDefaultSetup(2);% Get the screen numbers. This gives us a number for each of the screens% attached to our computer.screens = Screen('Screens');% To draw we select the maximum of these numbers. So in a situation where we% have two screens attached to our monitor we will draw to the external% screen.screenNumber = max(screens);% Define black and white (white will be 1 and black 0). This is because% in general luminace values are defined between 0 and 1 with 255 steps in% between. All values in Psychtoolbox are defined between 0 and 1white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);% Do a simply calculation to calculate the luminance value for grey. This% will be half the luminace values for whitegrey = white / 2;% Open an on screen window using PsychImaging and color it grey.[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
```

======

  

Thanks

HD

---

<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:** [May 23, 2019, 7:54pm UTC](https://psychtoolbox.discourse.group/t/cannot-terminate-the-screen-with-a-button/29/2 "2019-05-23T19:54:37Z")

</div>

Add, e.g. a WaitSecs(2) for 2 second screen open, and then sca again
  

Also recommended is to wrap all your code in a try-catch block. If things error, the code in the catch block will be run, where you can then put sca to at least kill the screen

  

> On Thu, May 23, 2019 at 9:49 PM [2x34umvpzxcznpgg6tadocppxjo5xonbmz2xeczw@...](mailto:2x34umvpzxcznpgg6tadocppxjo5xonbmz2xeczw@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> 
> > Hi,
> > 
> > I am trying to learn MATLAB on my own and has struggled through with my mac airbook, I have switched to Linux for it and it works fine except one thing that I cannot terminate screen with any button after I opened it. And Idk why. I could terminate it with sca code but I do not want to do it for purposes of next codes. I also try ErrorCathDemo and it show like this;
> > 
> > This is the code I am trying. After it opens grey screen, I am not able to close it :(   
> > 
> > ```
> > % Clear the workspace and the screensca;
> > closeall;
> > clearvars;% Here we call some default settings for setting up PsychtoolboxPsychDefaultSetup(2);% Get the screen numbers. This gives us a number for each of the screens% attached to our computer.screens = Screen('Screens');% To draw we select the maximum of these numbers. So in a situation where we% have two screens attached to our monitor we will draw to the external% screen.screenNumber = max(screens);% Define black and white (white will be 1 and black 0). This is because% in general luminace values are defined between 0 and 1 with 255 steps in% between. All values in Psychtoolbox are defined between 0 and 1white = WhiteIndex(screenNumber);
> > black = BlackIndex(screenNumber);% Do a simply calculation to calculate the luminance value for grey. This% will be half the luminace values for whitegrey = white / 2;% Open an on screen window using PsychImaging and color it grey.[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
> > ```
> > 
> > ======
> > 
> >   
> > 
> > Thanks
> > 
> > HD

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [May 26, 2019, 5:58am UTC](https://psychtoolbox.discourse.group/t/cannot-terminate-the-screen-with-a-button/29/3 "2019-05-26T05:58:56Z")

</div>

On Thu, May 23, 2019 at 9:52 PM  
[2x34umvpzxcznpgg6tadocppxjo5xonbmz2xeczw@...](mailto:2x34umvpzxcznpgg6tadocppxjo5xonbmz2xeczw@...) [PSYCHTOOLBOX]  
\<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  

> \> Hi,  
> \> I am trying to learn MATLAB on my own and has struggled through with my mac airbook, I have switched to Linux for it and it works fine except one thing that I cannot terminate screen with any button after I opened it. And Idk why. I could terminate it

-\> Congratulations for switching to Linux. Good for you!  
  
with sca code but I do not want to do it for purposes of next codes. I  
also try ErrorCathDemo and it show like this;  

> \> This is the code I am trying. After it opens grey screen, I am not able to close it :(  
> \>

Can you clarify why you don't want to use sca? Because in your  
example, obviously if you don't add a command to close the onscreen  
window, it will not magically close by itself. How would the poor  
thing know you want it to close without a command to do so? There's  
Screen('Close', window) if you just want to close that specific  
window, or Screen('CloseAll') if you want to close all windows. Both  
close one or all windows, without doing all the other cleanup work of  
sca() - assuming you want to prevent some of the cleanup work for  
happening?  
  
Other than that, what Diederick recommends.  
-mario  
  

> \> % Clear the workspace and the screen  
> \> sca;  
> \> close all;  
> \> clearvars;  
> \>  
> \> % Here we call some default settings for setting up Psychtoolbox  
> \> PsychDefaultSetup(2);  
> \>  
> \> % Get the screen numbers. This gives us a number for each of the screens  
> \> % attached to our computer.  
> \> screens = Screen('Screens');  
> \>  
> \> % To draw we select the maximum of these numbers. So in a situation where we  
> \> % have two screens attached to our monitor we will draw to the external  
> \> % screen.  
> \> screenNumber = max(screens);  
> \>  
> \> % Define black and white (white will be 1 and black 0). This is because  
> \> % in general luminace values are defined between 0 and 1 with 255 steps in  
> \> % between. All values in Psychtoolbox are defined between 0 and 1  
> \> white = WhiteIndex(screenNumber);  
> \> black = BlackIndex(screenNumber);  
> \>  
> \> % Do a simply calculation to calculate the luminance value for grey. This  
> \> % will be half the luminace values for white  
> \> grey = white / 2;  
> \>  
> \> % Open an on screen window using PsychImaging and color it grey.  
> \> [window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);  
> \>  
> \> ======  
> \>  
> \> Thanks  
> \> HD  
> \>  
> \>  
> \>
