Can the start position for ginput() crosshairs be chosen?
2 ビュー (過去 30 日間)
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?
Voss 2022 年 5 月 4 日
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:
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).