MATLAB GUI - Select a point on a plot and run a function with a push button
4 May 2020
1 Answer
9 Views (30 days)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
What I want to do is to get which point the user has clicked on a given plot, and then, when he clicks on a push button, a function that receives as args the (X,Y) coordinates of that point will run. If there's no point selected on the plot, a warning msg will show, telling the user to select a point.
Is this possible?
2 Comments
Geoff Hayes
on 5 May 2020
Edited: Geoff Hayes
on 5 May 2020
Pedro - do you need the push button? Or could the user just click on a point in the axes/plot and have the code execute immediately (with that point)? Also, are you using App Designer, GUIDE or programmatically creating your GUI?
I'm using GUIDE.
If it's possible to use the push button that way it'd be preferable, because this other function might take a while to run, and if it executes everytime the user presses the plot, it wouldn't be optimal. But, if that's impossible, just clicking on the plot and having the code to execute should be fine too.
Accepted Answer
Geoff Hayes
on 5 May 2020
Pedro - in the OpeningFcn for your GUI, add a button down callback for your axes:
set(handles.axes1, 'ButtonDownFcn',@axesButtonDownCallback);
The name of the callback function will be axesButtonDownCallback. The callback will look like
function axesButtonDownCallback(hObject, eventdata)
handles.currentPoint = get(hObject,'CurrentPoint');
guidata(hObject, handles);
We get the CurrentPoint property of the axes (the hObject) and save that to a field in the handles structure. We then need to save the updated structure with guidata. This is important - if you don't do this, then the other callbacks won't have access to this fields that we've added to handles. The pushbutton callback will then be
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'currentPoint') || isempty(handles.currentPoint)
fprintf('You need to choose a point!\n');
else
currentPoint = handles.currentPoint;
handles.currentPoint = [];
guidata(hObject,handles);
% do something with point
end
Note how we check to see if the 'currentPoint' field exists in the handles structure or if it does exist, whether it is empty. If either is true, then we prompt the user to choose a point (replacing the fprintf with a dialog). If we do have a point, then we extract it from the structure and then reset that field in the structure so that the user will be prompted to choose a new point next time the button is pressed. (Note that again, calling guidata is important.)
10 Comments
Pedro Augusto de Castro e Castro
on 5 May 2020
Edited: Pedro Augusto de Castro e Castro
on 5 May 2020
Thank you for your answer!
I have several axes, and I already have a ButtonDownFcn for each one of them, to show a custom data tip when a point is clicked. How'd I do it then?
Geoff Hayes
on 5 May 2020
So when you press a button, which axes does it correspond to? Or does each axes have a button?
Pedro Augusto de Castro e Castro
on 5 May 2020
Edited: Pedro Augusto de Castro e Castro
on 5 May 2020
I have only one button, that will be used for all the axes. But in each axes, I have several plots too. I need to know which axes and which plot the user is clicking.
I was able to do it for one axes, but how do I know which plot is being clicked?
Other thing, if I do this once, it seems to work. But, if I click on another point and then click again on tha push button if does not work (the msg appears 'You need to choose a point!\n').
Geoff Hayes
on 5 May 2020
Well when you save the current point (to the handles object), you would need to save the handle to the axes that it corresponds to.
function axesButtonDownCallback(hObject, eventdata)
handles.currentPoint = get(hObject,'CurrentPoint');
handles.axesForCurrentPoint = hObject;
guidata(hObject, handles);
As for not being able to get this to work again, either you would need to post your code or use the MATLAB debugger to figure out what is going on.
I don't know if this code will help because it needs a data base and other functions to run (and it's in portuguese too). But here is the main GUI function.
I still don't understand how the code will know which plot is being clicked.
Thanks agains for helping me.
Geoff Hayes
on 5 May 2020
If all of your graph_XX_ButtonDownFcn callbacks work, then you could add the following to each (!) one
handles.currentPoint = get(hObject,'CurrentPoint');
handles.axesForCurrentPoint = hObject;
guidata(hObject, handles);
So now you will know which axes the user last pressed the mouse button down on, axesForCurrentPoint, and where, currentPoint. Now add this code
if ~isfield(handles, 'currentPoint') || isempty(handles.currentPoint)
fprintf('You need to choose a point!\n');
else
currentPoint = handles.currentPoint;
axesForCurrentPoint = handles.axesForCurrentPoint;
handles.currentPoint = [];
handles.axesForCurrentPoint = [];
guidata(hObject,handles);
% do something with point
end
to your push button (I'm not sure which one).
Pedro Augusto de Castro e Castro
on 5 May 2020
Edited: Pedro Augusto de Castro e Castro
on 5 May 2020
ok, thanks!
There's a problem though. Instead of showing the value in red (the real value of the point in the curve), the currentPoint is showing another value, in blue (perhaps the first value that I click on the axes, and not the value of the point on the plot).
In this case, it should show (1.3764 , 0.986265).

I guess the code only runs the axes button down fcn once, and then it gets only the first point I clicked. Also, because of this, the code doesn't work when I press the push button again.
Geoff Hayes
on 5 May 2020
Well if you want the same point, you'll somehow need to use the data from the myupdatefcn....which you can probably do by passing the hObject as a parameter to this function (and then getting and updating the handles structure from withih it). Maybe (and this isn't tested)
set(handles.dcm,'UpdateFcn',@(t,e) myupdatefcn(t,e, handles.PerdasResults, hObject));
with
function myupdatefcn(t,e,results, hObject)
handles = guidata(hObject);
handles.currentPoint = ;% whatever
handles.axesForCurrentPoint = hObject;
guidata(hObject, handles);
% rest of your code
end
As for the code doesn't work when I press the push button again. without seeing your code I won't be able to tell you what is going wrong. Again, try using the debugger to figure it out.
Thanks for your help man! Really helpful!
Just another quick question: how do I debbug, if I'm using a GUI that depends on the user inputs?
Geoff Hayes
on 5 May 2020
no problem! For debugging, just put breakpoints at the lines of code that you are interested in.
More Answers (0)
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
See Also
on 4 May 2020
on 5 May 2020
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)