MATLAB Answers

Help with graphical input on a GUI axes

29 views (last 30 days)
HpW
HpW on 10 Apr 2018
Commented: HpW on 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 Comments

Sign in to comment.

Accepted Answer

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

  1 Comment

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

Sign in to comment.

More Answers (1)

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

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by