David Pfau
David Pfau

Reputation: 813

Update figure while waiting for event in Matlab?

I'd like to be able to set parameters in a program in real time in Matlab using mouse input. I have video playing continuously in one figure, and I'd like to be able to click on certain points in that figure to update. However, when I call ginput, the figure stops updating until I click. Right now my code looks something like this:

while 1
    frame = step(FrameReader);
    image(frame);
    [x,y] = ginput(1);
    pause(0.1);
end

Is there another way to introduce a mouse click listener to a figure than using ginput, or another way to call ginput that will allow the movie to run seamlessly in the background?

Upvotes: 1

Views: 968

Answers (1)

p8me
p8me

Reputation: 1860

You can use 'ButtonDownFcn' property (of the figure or axes) as mentioned by grantnz. ginput is more handy when there is a non real-time task.

First of all, note that high level functions such as plot, imshow, image etc, will reset most of the axes properties such as 'HitTest' and 'ButtonDownFcn'. Thus after each time you use such functions you should update the required axes handle properties. In general you should avoid using such high level functions in a high frequency loop for better performance.

In addition to setting 'ButtonDownFcn' of the axes implied by grantnz you can use the 'ButtonDownFcn' of the figure. Note that in second case you should turn off the selectability of the axes (set 'HitTest' of axes to Off).

Here is a dummy animation, in which you can click on the animated axes and see the 'CurrentPoint' of the axes.

% Stop button
uicontrol(...
    'Style','pushbutton', 'String', 'Stop',...
    'Units','Normalized', 'Position', [0.4 0.1 0.2 0.1],...
    'Callback', 'run = 0;');

% Axes
ax = axes(...
    'Units','Normalized',...
    'OuterPosition', [0 0.2 1 0.8]);

run = 1;
t = 0;
while run
    t = t + 0.01; x = t:0.01:t+2;
    h = plot(ax, x, sin(x));
    set(ax, 'ButtonDownFcn', 'get(ax, ''CurrentPoint'')');
    xlim([x(1) x(end)]); ylim([-1 1]);
    pause(0.01);
end

Note that 'ButtonDownFcn' is updated each time after plot.

It's also possible to get the 'CurrentPoint' of the figure, if so, you should set Normalized units for figure to get the normalized position of 'CurrentPoint'.

Upvotes: 1

Related Questions