Hasan Haj
Hasan Haj

Reputation: 83

GUI matlab adding zoom button for zooming the axes

In my GUI there is an axes, I want to add a buttons to zoom in/out the plotted signal in the axes. But I didn't know how to code these buttons. Any help

Upvotes: 0

Views: 3787

Answers (3)

sco1
sco1

Reputation: 12214

Based on your comment I've created a quick little GUI to illustrate the process.

This code assumes you have MATLAB 2014b or newer:

function heyitsaGUI
% Set up a basic GUI
h.mainwindow = figure( ... % Main figure window
    'Units','pixels', ...
    'Position',[100 100 800 800], ...
    'MenuBar','none', ...
    'ToolBar','none' ...
    );

h.myaxes = axes( ...
    'Parent', h.mainwindow, ...
    'Position', [0.1 0.15 0.8 0.8] ...
    );

h.zoomtoggle = uicontrol( ...
    'Style', 'togglebutton', ...
    'Parent', h.mainwindow, ...
    'Units', 'Normalized', ...
    'Position', [0.4 0.05 0.2 0.05], ...
    'String', 'Toggle Zoom', ...
    'Callback', {@myzoombutton, h} ... % Pass along the handle structure as well as the default source and eventdata values
    );

% Plot some data
plot(1:10);
end

function myzoombutton(source, ~, h)
% Callbacks pass 2 arguments by default: the handle of the source and a
% structure called eventdata. Right now we don't need eventdata so it's
% ignored.
% I've also passed the handles structure so we can easily address
% everything in our GUI
% Get toggle state: 1 is on, 0 is off
togglestate = source.Value;

switch togglestate
    case 1
        % Toggle on, turn on zoom
        zoom(h.myaxes, 'on')
    case 0
        % Toggle off, turn off zoom
        zoom(h.myaxes, 'off')
end
end

Which will generate you something that looks like this:

Sample GUI

Since you're new I would highly recommend you read through MATLAB's GUI building documentation, it walks you through building both programmatic GUIs and GUIs using GUIDE. What I've done here is create a custom little Callback function, myzoombutton, that allows you to toggle the zoom on and off for your axis object. There are a few good ways to answer your question, this is only one and not necessarily the best. I like using this method because it gives me the framework to add other actions and calculations based on the state of the toggle button.

Take a look at the code and the linked documentation, it should hopefully help you get started.

Upvotes: 1

siliconwafer
siliconwafer

Reputation: 732

Use the uitoolbar function to create a custom toolbar. Then, use uipushtool to create a zoom-in, and a zoom-out button. In your callback function for the buttons, you can get the current axes limits, and then scale them by some factor.

Upvotes: 0

il_raffa
il_raffa

Reputation: 5175

In a Figure the zoom in and zoom out button are already available in the Figure toolbar The toolbar can be made visible through the Figure Menu:

View -> Figure Toolbar

Hope this helps.

Upvotes: 0

Related Questions