Clear Filters
Clear Filters

How to create zoom in/out for GUI MATLAB

13 views (last 30 days)
amir nemat
amir nemat on 12 Aug 2017
Commented: Geoff Hayes on 25 Apr 2022
I have developed two buttons for zoom in and zoom out. However, two problems are arising. Firstly, although the zoom in would be appeared in axes, zoom out would not appeared in axes and zoom in would be remained. Secondly, I cannot control zoom out factor. When I put the button for zoom out suddenly it goes to first situation.
My developed code is as followings:
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
handles.aa=get(handles.pushbutton14,'Value');
if handles.aa~=0
axes(handles.axes1)
zoom on
end
if handles.aa==0
axes(handles.axes1)
zoom off;
end
guidata(hObject,handles);
% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
handles.aa=get(handles.pushbutton15,'Value');
if handles.aa~=0
axes(handles.axes1)
zoom out
zoom(handles.aa);
end
if handles.aa==0
axes(handles.axes1)
zoom off;
end
guidata(hObject,handles);
  1 Comment
Adam
Adam on 14 Aug 2017
Is there a reason you can't just use the standard toolbar zoom in and out functionality?

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 12 Aug 2017
amir - it isn't entirely clear (to me) why your code for each pushbutton is checking something called handles.aa and then deciding whether to enable or disable zoom, especially as you have stated that you have two buttons which (I thought) one would be to enable zoom and the other to disable it. The following will do that for you (if you assume that pushbutton1 enables zoom and pushbutton2 disables it)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isempty(handles.hZoom)
handles.hZoom = zoom;
guidata(hObject, handles);
end
if ~strcmp(get(handles.hZoom,'Enable'), 'on')
handles.axesLimits = get(handles.axes1,{'xlim','ylim'});
guidata(hObject, handles);
set(handles.hZoom, 'Enable', 'on');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isempty(handles.hZoom)
set(handles.hZoom, 'Enable', 'off');
set(handles.axes1, {'xlim','ylim'}, handles.axesLimits);
end
In pushbutton1, we create a zoom object and store its handle to the handles structure. We then enable it (first saving the current axes limits) and so now the user can zoom in or out as he or she sees fit. In pushbutton2, we disable to the zoom and restore the original axes limits.
Please see the attached for an example. It may be that you are hoping to control the zoom in and out using these two buttons. If that is the case, then you would need to save the last known axes limits (kind of like we are doing now) before zooming in so that when you zoom out, you can restore the appropriate axes limits. If zooming in multiple times, then you would need to create an array of these axes limits and then when zooming out, use the last axes limits values that have been added to this array.
  4 Comments
sanket neharkar
sanket neharkar on 25 Apr 2022
can we use slider for zoom in and zoom out
Geoff Hayes
Geoff Hayes on 25 Apr 2022
I suspect you could use a slider to zoom in and out.

Sign in to comment.

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!