Can the start position for ginput() crosshairs be chosen?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!