这是我第一次创建matlab GUI。
我想通过使用matlab点击它来获取图像中像素的坐标,我创建了一个包含轴的Matlab GUI,并且轴通过以下代码包含图像:
function axes1_CreateFcn(hObject, eventdata, handles)
axes(hObject);
I = imread('cameraman.tif');
imshow(I);
和ButtonDownFcn
获取点击像素的坐标:
function axes1_ButtonDownFcn(hObject, eventdata, handles)
handles.xy1 = round(get(handles.axes1,'Currentpoint'));
x1 = handles.xy1(1,1);
y1 = handles.xy1(1,2);
问题是当我点击图片时ButtonDownFcn
没有调用,但当我从CreateFcn
函数中删除代码时,会调用ButtonDownFcn
。
如何显示图像并同时保持ButtonDownFcn
正常工作?
感谢,
答案 0 :(得分:1)
需要设置这些功能。像这样的东西会起作用:
set(hFigure,'ButtonDownFcn', @axes1_ButtonDownFcn);
答案 1 :(得分:1)
这仅仅是因为在Matlab GUI
上执行函数imshow
时Axes
的奇怪行为,它会重置Axes的属性。
您正在看到图像和冲浪命令安静地对轴属性进行更改的副作用。 [通过Mathworks:here]
尝试使用此代码显示图片:
function axes1_CreateFcn(hObject, eventdata, handles)
axes(hObject);
tag = get(hObject,'Tag');
I = imread('cameraman.tif');
imshow(I);
set(hObject,'Tag',tag);
set(hFigure,'ButtonDownFcn', @axes1_ButtonDownFcn);
end
如果您只想查看坐标,请使用Data Cursor
工具,将其从GUI
添加到Toolbar Editor
,然后使用它来浏览Axes
绘图或图片并显示来自点击位置的信息,您甚至可以更改其操作代码。