Need to extract data and exit function - my function never ends

Asked by Myles on 27 Jun 2012
Latest activity Commented on by Walter Roberson on 27 Jun 2012

I have a function which allows me to draw a line over top of a current figure. The function begins by plotting a point and then continuously updates a second point to draw a dynamic line which leads to the current mouse location - which then terminates if the user right clicks. My problem is that when I run this function within another script, that script skips over the function before it finishes - in fact I don't think the function is able to end the way I have it written, but I'm still relatively novice at MATLAB and can't figure out how to fix my code for this problem. I'm also extracting the information for the line by using getappdata and setappdata, which probably isn't the best way to do it. Any help would be greatly appreciated, Thanks.

%% ---------------------------------------------------------

function [x,y] = junk
% axis([0 10 0 10])
hold on
f=gcf;
[x,y] = ginput(1);
hl = line(x,y);
ah = gca;
set(ah, 'DrawMode', 'fast');
set(f,'WindowButtonMotionFcn',@movingLine);
      function [xdat,ydat] = movingLine(src,~)            
           [xn,yn] = disp_point(ah);                     
           xdat = [x,xn];                                    
           ydat = [y,yn];                                     
           set(hl,'XData',xdat,'YData',ydat);                 
           set(f,'WindowButtonDownFcn', @outputLines)
      function [xdat,ydat] = outputLines(src,evnt)
          if strcmp(get(src,'SelectionType'),'alt')      
              xdat = [x,xn];
              ydat = [y,yn];
              set(hl,'Xdata',xdat,'Ydata',ydat)
              setappdata(src, 'hLine', hl);
              set(f, 'WindowButtonDownFcn','')
              set(f, 'WindowButtonMotionFcn','')
          end
      end
      function [x,y] = disp_point(ah)                             
        cp = get(ah,'CurrentPoint');                                   
        x = cp(1,1);y = cp(1,2);                                      
      end
  end

0 Comments

Myles

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 27 Jun 2012
Accepted answer

After your line

set(f,'WindowButtonMotionFcn',@movingLine);

add

uiwait(f, 'WindowButtonMotionFcn')

This tells it to wait until f's WindowButtonMotionFcn property changes, which is something that you do in the callback when you got the data you need.

4 Comments

Tom on 27 Jun 2012

Walter may have meant the waitfor function?

Myles on 27 Jun 2012

Yup, worked perfectly. Thanks you guys!

Walter Roberson on 27 Jun 2012

Darn, and I even had the waitfor() documentation open when I mis-typed that!

Walter Roberson

Contact us