This is the next in a series of advanced topics in MATLAB. My definition of “Advanced” is somewhat nebulous and arbitrary, so even newer users can give this a watch.
Two and a half minute video shows how to use buttondownfcn to get the current point. This can be a fundamental skill that underlies many advanced GUIs.
I am having problem replicating the great example you have provided here. Specifically, I have create the following in an m file in an attempt to replicate what you have here…
I have done this kind of thing before, it does require some advanced maneuvers. Mostly it is about using ASSIGNIN to redefine a variable in the workspace as you modify it graphically. You need to place that function call in the end of the callback that modifies the data.
I try to use ButtonDownFcn to get information about image pixels in the current figure. The function works well before the image showed in axes, but after the image loading into the axes, the ButtonDownFcn does not work.
here is the code
%%%%%
function pushbutton_input_vimage_Callback(hObject, eventdata, handles)
%%%%
function Visible_iamge_ButtonDownFcn(hObject, eventdata, handles)
handles.mouse_pos_fig=get(handles.figure1,’currentpoint’);
axes_area=get(handles.Visible_iamge,’position’);
if (handles.mouse_pos_fig(1)(axes_area(1))) && …
(handles.mouse_pos_fig(2)(axes_area(2)))
handles.mouse_pos=get(hObject,’currentpoint’);
else
handles.mouse_pos=-1;
end
b=handles.mouse_pos(1,1:2);
if handles.mouse_track==2
set(handles.text_A,’string’,int2str(b));
elseif handles.mouse_track==3
set(handles.text_B,’string’,int2str(b));
elseif handles.mouse_track==4
set(handles.text_C,’string’,int2str(b));
end
Some of the graphics functions create a new axes and replace the old one. In doing this, the old callbacks, and certain other attributes are removed. You need to set them again.
As Doug said in comment 7, by default some of the high-level graphics functions (including image) clear many of the properties of the axes. You can restore those properties after calling IMAGE (as Doug suggested) or you can SET the axes NextPlot property to ‘replacechildren’ before calling IMAGE.
A similar type of problem, but with PLOT and the Tag property instead of IMAGE and the ButtonDownFcn property, is described in this document on our support website; the solution is the same.
Thanks for another great tutorial.
If my plot were actually a 2-D figure, would it be possible to get the (x,y) and also the intensity at that spot (x/255)?
Thanks,
Daphne
I am having problem replicating the great example you have provided here. Specifically, I have create the following in an m file in an attempt to replicate what you have here…
function[] = graphinteract
figure(1)
plot(peaks)
set(gca,’buttondwnfcn’, @clicker)
function clicker(gcbo,eventdata,handles)
disp(get(gca,’Currentpoint’))
However, I always get the following error…
??? There is no ‘buttondwnfcn’ property in the ‘axes’ class.
Error in ==> graphinteract at 5
set(gca,’buttondwnfcn’, @clicker)
Any idea what I am doing wrong?
CW,
‘buttonDownFcn’ is spelled wrong in the above code.
Doug
How can this function/code be used to modify the data by dragging it?
If it cant be used to do this, then what function allows me to drag data points from a plot and modify the graph (and the new data) on the fly.
Thanks
Zach
Zach,
I have done this kind of thing before, it does require some advanced maneuvers. Mostly it is about using ASSIGNIN to redefine a variable in the workspace as you modify it graphically. You need to place that function call in the end of the callback that modifies the data.
-Doug
Doug,
I try to use ButtonDownFcn to get information about image pixels in the current figure. The function works well before the image showed in axes, but after the image loading into the axes, the ButtonDownFcn does not work.
here is the code
%%%%%
function pushbutton_input_vimage_Callback(hObject, eventdata, handles)
[vname,vpath]=uigetfile(’*.bmp; *.img; *.jpg; *.tif’);
vimage=[vpath vname];
handles.user.plot_vimage=imread(vimage);
axes(handles.Visible_iamge)
image(handles.user.plot_vimage)
guidata(hObject, handles);
%%%%%
function popupmenu_vpoints_Callback(hObject, eventdata, handles)
if get(hObject, ‘value’)==2
handles.mouse_track=2;
elseif get(hObject, ‘value’)==3
handles.mouse_track=3;
elseif get(hObject, ‘value’)==4
handles.mouse_track=4;
end
guidata(hObject, handles);
%%%%
function Visible_iamge_ButtonDownFcn(hObject, eventdata, handles)
handles.mouse_pos_fig=get(handles.figure1,’currentpoint’);
axes_area=get(handles.Visible_iamge,’position’);
if (handles.mouse_pos_fig(1)(axes_area(1))) && …
(handles.mouse_pos_fig(2)(axes_area(2)))
handles.mouse_pos=get(hObject,’currentpoint’);
else
handles.mouse_pos=-1;
end
b=handles.mouse_pos(1,1:2);
if handles.mouse_track==2
set(handles.text_A,’string’,int2str(b));
elseif handles.mouse_track==3
set(handles.text_B,’string’,int2str(b));
elseif handles.mouse_track==4
set(handles.text_C,’string’,int2str(b));
end
guidata(hObject,handles);
Lorraine,
Some of the graphics functions create a new axes and replace the old one. In doing this, the old callbacks, and certain other attributes are removed. You need to set them again.
Doug
Doug,
Thanks, but how can i do that?
I tried to get the new axes by
h = findobj(handles.Visible_iamge,…),but it returned nothing
Lorraine
Lorraine,
When you call the image command, capture the handle to that, then get the parent.
Doug
Doug,
It works now, thanks : )!
Lorraine
Lorraine,
As Doug said in comment 7, by default some of the high-level graphics functions (including image) clear many of the properties of the axes. You can restore those properties after calling IMAGE (as Doug suggested) or you can SET the axes NextPlot property to ‘replacechildren’ before calling IMAGE.
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axes_props.html#NextPlot
A similar type of problem, but with PLOT and the Tag property instead of IMAGE and the ButtonDownFcn property, is described in this document on our support website; the solution is the same.
http://www.mathworks.com/support/solutions/data/1-168SX.html