MATLAB Answers

Help with graphical input on a GUI axes

조회 수: 23(최근 30일)
HpW
HpW 10 Apr 2018
댓글: HpW 13 Apr 2018
Hi. I have a simple GUI where a signal is loaded from a file, and then displayed in a GUI axes. I want to let the user click somewhere on the signal, get the x coordinate, and then draw a vertical line at this point. I have been trying to do this using the following code, but it has some issues:
% load signal....
axes(handles.signal_axis);
plot(signal)
hold on
dcm_obj = datacursormode;
set(dcm_obj,'DisplayStyle','datatip',...
'SnapToDataVertex','off','Enable','on')
% Select x coordinate where click on plot
waitforbuttonpress;
c_info = getCursorInfo(dcm_obj);
x = c_info.DataIndex
% draw line at x coordinate
line([x x], [ min(ylim) max(ylim) ], 'color', 'r', 'linewidth',3,'LineStyle',':')
hold off
The issues with this are: 1. once the user clicks on the signal to get the data index (x value), if they click again it does not work since ive exited the waitforbuttonpress section of code. I therefore have to reload the entire signal to choose another x value/draw another line in a new location.
2. If i click anywhere other than on the actual signal, I get an error.
Can someone please help me with this? Id ideally like to be able to click multiple times on the axes displaying the signal and each time it should allow me to get the data index and draw a line there.
Also, is there a way to do this just by clicking somewhere in the axes object rather than having to click exactly on the plotted signal?
thanks for the help!!
h

  댓글 수: 0

로그인 to comment.

채택된 답변

Walter Roberson
Walter Roberson 10 Apr 2018
You can use a loop of ginput(1). Or you can use an axes ButtonDownFcn callback.

  댓글 수: 1

HpW
HpW 13 Apr 2018
thanks!! Did ginput(1) with ButtonDownFcn - works great!

로그인 to comment.

추가 답변(1개)

Tamir Suliman
Tamir Suliman 10 Apr 2018
I think you may need to implement some while loop or logic to read the cursor location when is changed

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by