按钮放大并缩小GUI

时间:2016-02-10 09:57:23

标签: matlab user-interface matlab-guide

我需要代码来按下按钮"放大","缩小"为我的形象。试图用这个但是错了。请帮我。我使用MATLAB Gui。

enter image description here

function btnZoomIn_Callback(hObject, eventdata, handles)
uicontrol('Style','pushbutton','String','ZoomIn','Units','pixels',...
'Position',[90 10 60 20],'Enable','off',...
'Tag','btnZoomIn','Callback',@btnZoomIn_Callback);
h = guihandles(hObject);
set(h.btnZoomOut,'Enable','on')
data = guidata(hObject);
data.magnif = data.magnif+1;
guidata(hObject, data)

function btnZoomOut_Callback(hObject, eventdata, handles)
uicontrol('Style','pushbutton','String','Zoom Out','Units','pixels',...
'Position',[160 10 60 20],'Enable','off',...
'Tag','btnZoomOut','Callback',@btnZoomOut_Callback);

h = guihandles(hObject);
data = guidata(hObject);
if data.magnif > 1
 data.magnif = data.magnif-1;
  if data.magnif == 1

1 个答案:

答案 0 :(得分:3)

如果你想对它感到厚颜无耻(希望这个成语可以翻译......),你可以将你的教授指向内置的缩放按钮。

(非GUIDE)示例:

f = figure;
ax = axes('Parent', f, 'Units', 'Normalized', 'Position', [0.1 0.18 0.8 0.8]);
A = imread('ngc6543a.jpg'); % Read a built-in image as a sample
image(A, 'Parent', ax);

yay

但是,如果您需要一个认真的答案,请参阅MATLAB的zoom函数,您可以将其添加到按钮回调中。

扩展上面的例子:

f = figure;
ax = axes('Parent', f, 'Units', 'Normalized', 'Position', [0.1 0.18 0.8 0.8]);
A = imread('ngc6543a.jpg'); % Read a built-in image as a sample
image(A, 'Parent', ax);

zoomonbutton = uicontrol('Parent', f, ...
                         'Style', 'pushbutton', ...
                         'Units', 'Normalized', ...
                         'Position', [0.1 0.02 0.4 0.1], ...
                         'String', 'Zoom On', ...
                         'Callback', 'zoom on' ...
                         );

zoomoffbutton = uicontrol('Parent', f, ...
                         'Style', 'pushbutton', ...
                         'Units', 'Normalized', ...
                         'Position', [0.5 0.02 0.4 0.1], ...
                         'String', 'Zoom Off', ...
                         'Callback', 'zoom off' ...
                         );

yay2

按下“开启”按钮可启用交互式缩放功能。来自the documentation

  

zoom on启用交互式缩放功能。交互式缩放时   在图中启用,在光标处按下鼠标按钮   在一个轴内缩放到该点或从该点下面的点   老鼠。缩放会更改轴限制。使用缩放模式时,

Zoom in by positioning the mouse cursor where you want the center of the plot to be and either

    Press the mouse button or

    Rotate the mouse scroll wheel away from you (upward).

Zoom out by positioning the mouse cursor where you want the center of the plot to be and either

    Simultaneously press Shift and the mouse button, or

    Rotate the mouse scroll wheel toward you (downward).

按下“关闭”按钮将关闭此交互模式。

希望这可以帮助您朝着正确的方向前进。我建议你研究MATLAB的文档,它非常全面并且有很多例子。