How to create zoom in/out for GUI MATLAB

16 ビュー (過去 30 日間)
amir nemat
amir nemat 2017 年 8 月 12 日
コメント済み: Geoff Hayes 2022 年 4 月 25 日
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 件のコメント
Adam
Adam 2017 年 8 月 14 日
Is there a reason you can't just use the standard toolbar zoom in and out functionality?

サインインしてコメントする。

回答 (1 件)

Geoff Hayes
Geoff Hayes 2017 年 8 月 12 日
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 件のコメント
sanket neharkar
sanket neharkar 2022 年 4 月 25 日
can we use slider for zoom in and zoom out
Geoff Hayes
Geoff Hayes 2022 年 4 月 25 日
I suspect you could use a slider to zoom in and out.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Exploration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by