PickPointNonblockin​g - pick points in axes without blocking

Version 1.0.1 (2.51 KB) by RST
The PickPointNonblocking class allows the user to pick points from an axes without blocking the GUI with uiwait()
5 Downloads
Updated 14 Mar 2024

View License

Introduction
The PickPointNonblocking class allows the user to pick points from an axes without blocking the GUI with uiwait(). PickPointNonblocking is demonstrated with both figure() and uifigure() based apps.
Background
I have a large figure-based app that receives, in real time, input from a frame grabber, a robot and various other instruments. I want to select points in my live image without blocking the GUI. But both getpts() and the newer drawpoint() block.
This sounds simple - just hook into the axes' ButtonDownFcn. But the details are tricky. We need to:
  • turn off the axes toolbar when picking
  • change the cursor while it is picking over the axes
  • pick a point on the background of the axes, not a point on one of the lines or whatever else is plotted in the axes.
PickPointNonblocking evolved from these requirements. It requires a handle to the axes in its constructor and calls back to:
  • PointPickedFcn with the point selected on left mouse click, and to
  • CancelledFcn on right mouse click.
It can also notify events 'PointPicked' and 'Cancelled'.
Simple demonstration
Like this:
plot(1:10,1:10)
hold on
pp = PickPointNonblocking( gca, ...
@(pt)fprintf(1, 'point picked %.2f %.2f\n', pt(1), pt(2)), ...
@()disp('pick cancelled') );
pp.startPick()
shg
which yeilded
point picked 3.69 6.52
point picked 5.77 4.21
point picked 4.41 6.50
point picked 3.73 5.80
point picked 7.07 2.59
point picked 4.62 6.08
pick cancelled
Note that while picking the axes toolbar is disabled and the cursor changes to a circle while over the axes and to a hand while outside the axes. Normal operation is restored when picking is stopped.
Real-time, figure()-based demonstration
I like to use guide() to lay out figures and then program then in a class. As in:
testPickPointFigClass()
It looks like this:
The line is updated at one-second intervals and it plot()s just the most recent point picked.
At startup, testPickPointFigClass()
  • loads the figure, drawn in guide
  • sets figure callbacks so we can delete things properly
  • sets the button callback
  • draws the rand() line
  • makes a timer that updates the rand() line
  • makes a PickPointNonblocking and connects it up
Notes on deleting figures() in classes
Putting figures() into classes is fiddly when it comes to tidying up. We want to delete() the class when we close the figure and also to delete() the figure when we close the class, which sounds like a recipie for recursion. testPickPointFigClass() shows how to do this safely by clearing the figures' callbacks during the main class' delete() function.
We also have to be careful to delete the timer. The timer must be deleted before we delete the figure otherwise the timer's callback will see the deleted() line object.
Real-time uifigure() demonstration app
testPickPointApp.mlapp is very similar in function to testPickPointFigClass.m.
testPickPointApp
but it appends picked points to a line rather than just having one point.
Notes on deleting AppDesigner apps
As of R2023a uifigures() provide a callback when the app's main uifigure() is closed, but it does not have a callback when the app itself is deleted from the command line. Nor is the app's delete() function accessible to the user. testPickPointApp.mlapp shows how to hook into the app's OnBeingDestroyed event so things like the timer() can be destroyed.

Cite As

RST (2024). PickPointNonblocking - pick points in axes without blocking (https://www.mathworks.com/matlabcentral/fileexchange/161111-pickpointnonblocking-pick-points-in-axes-without-blocking), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023a
Compatible with any release to R2023b
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.1

Fixed bug in PickPointsNonblocking for R2020a.

1.0.0