# How to use psychtoolbox with matlab gui?

**URL:** <https://psychtoolbox.discourse.group/t/how-to-use-psychtoolbox-with-matlab-gui/526>\
**Category:** Uncategorized\
**Created:** [January 10, 2011, 10:40pm UTC](https://psychtoolbox.discourse.group/t/how-to-use-psychtoolbox-with-matlab-gui/526 "2011-01-10T22:40:03Z")\
**Posts on this page:** 1\
**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:** [January 10, 2011, 10:40pm UTC](https://psychtoolbox.discourse.group/t/how-to-use-psychtoolbox-with-matlab-gui/526/1 "2011-01-10T22:40:03Z")

</div>

I wrote a script to show the coordinate of cursor in a matlab gui text box.It should update realtime but it only update when I double click the GUI figure.Any one can help me?Thanks in advance.  
  
Here is the code:  
  
function polarposition  
global data;  
%h1=uicontrol('style', 'text', 'string', 'mouse position', 'position', [10 10 150 50]);  
h1=uicontrol('style', 'text', 'position', [10 10 150 50]);   
h2=uicontrol('style', 'pushbutton', 'string', 'start polar grating', ...  
 'position', [10 70 150 50], 'callback',@polargrating,'interruptible','on');  
data.position=h1;  
end  
  
function polargrating(hObject, eventdata)  
  
ifis=1;  
numFrames=60;%both ifis and numFrames determine the temperal frequency  
wc=0.04\*2\*pi;  
wr=8;  
try  
 global data;  
 AssertOpenGL;  
 screens=Screen('Screens');  
 screenNumber=max(screens);  
 w=Screen('OpenWindow',screenNumber);  
 [width, height]=Screen('WindowSize', w);  
 Screen('BlendFunction', w, GL\_SRC\_ALPHA, GL\_ONE\_MINUS\_SRC\_ALPHA);  
  
 white=WhiteIndex(screenNumber);  
 black=BlackIndex(screenNumber);  
 gray=(white+black)/2;  
 if round(gray)==white  
 gray=black;  
 end  
 inc=white-gray;  
  
 s=min(width, height) ./ [4 4];  
 [x1,y1]=meshgrid(0:s(1)/2, -s(2)/2:s(2)/2);  
 [x2,y2]=meshgrid(-s(1)/2:-1, -s(2)/2:s(2)/2);  
  
 for i=1:numFrames  
 phase=(i/numFrames)\*2\*pi;  
 %m=sin(wc\*sqrt(x.\*x+y.\*y)+wr\*atan(y./x)+phase);   
 m1=sin(wc\*sqrt(x1.\*x1+y1.\*y1)+wr\*asin(y1./sqrt(x1.\*x1+y1.\*y1))+phase);   
 m2=sin(wc\*sqrt(x2.\*x2+y2.\*y2)+wr\*(pi-asin(y2./sqrt(x2.\*x2+y2.\*y2)))+phase);   
 m=[m2,m1];  
 gratingtex=(gray+inc\*m);  
 tex(i)=Screen('MakeTexture', w, gratingtex);  
 end  
 tavg=0;  
  
 ifi\_duration = Screen('GetFlipInterval', w);  
 movieDurationSecs=500; %stimulus duration  
  
 frameRate=Screen('FrameRate',screenNumber);  
 if(frameRate==0) % if MacOSX does not know the frame rate the 'FrameRate' will return 0.   
 frameRate=60; % 60 Hz is a good guess for flat-panels...  
 end   
  
 movieDurationFrames=round(movieDurationSecs \* frameRate / ifis);  
 movieFrameIndices=mod(0:(movieDurationFrames-1), numFrames) + 1;  
  
 priorityLevel=MaxPriority(w);  
 Priority(priorityLevel);  
  
 Screen('FillRect',w, gray);  
 vbl = Screen('Flip', w);  
 % hidecursor;  
  
 for i=1:movieDurationFrames  
 t1=GetSecs;  
 mxold=0;  
 myold=0;  
  
 WaitSecs(0.01);  
 [mx, my, buttons]=GetMouse(screenNumber);  
 myrect=[mx-s(1)/2 my-s(2)/2 mx+s(1)/2-1 my+s(2)/2-1];  
  
 Screen('DrawTexture', w, tex(movieFrameIndices(i)),[],myrect);  
 vbl=Screen('Flip', w, vbl + (ifis - 0.5) \* ifi\_duration);  
 mxold=mx;  
 myold=my;  
  
 xstr=num2str(mxold);  
 ystr=num2str(myold);  
 coordinate=[xstr,' ',ystr];  
 set(data.position,'string',coordinate);  
  
 [keyIsDown, secs, keyCode] = KbCheck;  
 if KbName(keyCode)=='q'  
 break;  
 end;  
  
 t1=GetSecs - t1;  
 if (i\>numFrames)  
 tavg=tavg+t1;  
 end;  
  
 end;  
  
 Priority(0);  
  
 tavg=tavg / (movieDurationFrames - numFrames)  
  
 % We're done: Close all windows and textures:  
 Screen('CloseAll');  
catch  
 Priority(0);  
 Screen('CloseAll');  
 psychrethrow(psychlasterror);  
end  
end
