Can the start position for ginput() crosshairs be chosen?

2 views (last 30 days)
Hello,
I have a GUIDE function that has a button defined for picking a location in an axes. When I click the button, the pick crosshairs don't appear until I move the cursor into the axes. A user can wonder what's going on and click the button more than once, not seeing a result. Is there a way to start the ginput() IN the relevant axes?
Thanks!
Doug

Accepted Answer

Voss
Voss on 4 May 2022
There is no option for that, as far as I can tell.
However, you can make a copy of ginput.m (let's say you call it my_ginput.m so it doesn't conflict with ginput.m) and modify the copy to call updateCrossHair (a local function within ginput.m) immediately after the crosshairs are created (i.e., don't wait until mouse movement occurs to update the crosshairs).
In R2017b, in ginput.m there's a function called setupFcn, and in that function is a call to createCrossHair, so right after that you'd put this line:
updateCrossHair(fig,initialState.CrossHair)
and that should get the crosshairs to turn on immediately, which may be good enough (they'll be centered on the pushbutton or wherever the cursor is when my_ginput is called).
In addition to that, if you want to programmatically move the cursor to the center of the relevant axes, you can do that by setting the groot property 'PointerLocation', e.g.:
set(0,'PointerLocation',ax_pos([1 2])+ax_pos([3 4])/2);
but note that PointerLocation is relative to screen position, so that ax_pos has to be relative to screen position as well, so you'd have to do something like:
fig_pos = getpixelposition(fig); % figure Position in pixels
ax_pos = getpixelposition(gca(),true); % axes Position in pixels, relative to figure (taking into account any panels the axes is nested in)
set(0,'PointerLocation',fig_pos([1 2])+ax_pos([1 2])+ax_pos([3 4])/2); % center of axes, relative to screen
If you put those three lines in my_ginput.m, after the figure's WindowButtonMotionFcn is set, which is right after the call to createCrossHair mentioned above (again, at least that's where it is in R2017b), then the crosshairs will show up immediately and be centered in the current axes (and if you do that you can remove the call to updateCrossHair above, since it'll be called automatically when PointerLocation is set).
  9 Comments
Douglas Anderson
Douglas Anderson on 6 May 2022
Yes, I have, thank you!
I had been trying to associate the crosshair with a specific axes, without success, but then figured out that all I needed to do was to find the right position within the main window!
Thanks again. Good solution.
Voss
Voss on 6 May 2022
Edited: Voss on 6 May 2022
You're welcome! Interesting problem. Glad it's working!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!