Daria Wolf
Daria Wolf

Reputation: 1

Pushbutton zoom in and zoom out in GUI

I need code to pushbuttons "zoom in", "zoom out" for my image. Trying to used this but wrong. Please, help me. I work with the 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

Upvotes: 0

Views: 1462

Answers (1)

sco1
sco1

Reputation: 12214

If you want to be cheeky about it (hopefully that idiom translates...), you could just point your professor to the built-in zoom buttons.

A (non-GUIDE) example:

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

However, if you need a serious answer, see MATLAB's zoom function, which you can add to your button callbacks.

To expand on the above example:

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

Where pushing the 'on' button turns on Interactive Zooming. From the documentation:

zoom on turns on interactive zooming. When interactive zooming is enabled in a figure, pressing a mouse button while your cursor is within an axes zooms into the point or out from the point beneath the mouse. Zooming changes the axes limits. When using zoom mode, you

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).

And pushing the 'off' button turns off this interactive mode.


Hopefully this helps you in the right direction. I would recommend you investigate MATLAB's documentation, it's very comprehensive and has many examples.

Upvotes: 3

Related Questions