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

1 回表示 (過去 30 日間)
Douglas Anderson
Douglas Anderson 2022 年 5 月 4 日
編集済み: Voss 2022 年 5 月 6 日
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

採用された回答

Voss
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:
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 件のコメント
Douglas Anderson
Douglas Anderson 2022 年 5 月 6 日
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 2022 年 5 月 6 日
編集済み: Voss 2022 年 5 月 6 日
You're welcome! Interesting problem. Glad it's working!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by