pass handle information to button down function

8 visualizzazioni (ultimi 30 giorni)
Sigrid Gerger
Sigrid Gerger il 29 Mag 2015
Risposto: Sigrid Gerger il 29 Mag 2015
I want to create a GUI, in which i plot a histogram into an axes. For this axes, I want a button down function to react in certain ways, depending on what bin of the histogram I am clicking at. I created the GUI with guide containing the axes 'handles.histo' and, through guide, made the histo_ButtonDownFcn(hObject, eventdata, handles) appear in the code.
function histo_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to histo (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%my added code that does certain things to other objects within this GUI
If i click on the axes itself, it works perfectly. But due to the problems that occur when it comes to adding the histogram, i need to additionally set histo_ButtonDownFcn as a ButtonDownFcn for the histogram h in the code.
axes(handles.histo);
h=imshow(myHistogram);
set(h,'ButtonDownFcn',@histo_ButtonDownFcn);
but for some reason, after adding this line, I don't have access to any handle anymore in the ButtonDownFcn, so if I would for instance like to change the string of some text field handles.text1 within the ButtonDownFcn
set(handles.text1,'String','newstring');
it would tell me:
Error using GUI_versuch_5>histo_ButtonDownFcn (line 108)
Not enough input arguments.
Error while evaluating image ButtonDownFcn
So it seems as if the input data (hObject, eventdata, handles) is not passed to the ButtonDownFcn anymore...
I would be grateful for any help or suggestions about how to solve this problem!

Risposte (1)

Sigrid Gerger
Sigrid Gerger il 29 Mag 2015
I finally found the solution to my problem: the missing data was only the handle-data, so seems like hObject was passed to the callback function. here is the working code for plotting a histogram into an axes, being able to click onto the bins themselves (not only the plain axes) and passing all handles to the callback function (with just a random example histogram):
axes(handles.histo);
x=[2,3,4,4,5,5,5,6,6,7,7,7,7,7,8,8,9,10,11,11];
hist(x,10);
set(handles.histo,'ButtonDownFcn',{@histo_ButtonDownFcn,handles})
child_handles = allchild(handles.histo);
set(child_handles,'HitTest','off');

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by