I have a gui and i want to have 4 buttons to pan my graph so one that goes left right up and down, how would i do this?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a gui and i want to have 4 buttons to pan my graph so one that goes left right up and down, how would i do this?
댓글 수: 0
채택된 답변
Matthew Eicholtz
2017년 5월 3일
The easy answer: I would suggest using the built-in pan tool in the figure toolbar.
The slightly more complicated answer: Try adapting the following code to your GUI. Here, I am creating a plot of some random numbers and using 4 uicontrols for pan buttons.
function panfigure()
% Setup figure
figure;
plot(rand(10,1),rand(10,1));
ax = gca;
set(ax,...
'Units','pixels',...
'Position',[140 100 400 300],...
'XLimMode','manual',...
'YLimMode','manual');
% Create pushbuttons for panning
uicontrol('Style','pushbutton','Position',[20 40 40 20],'String','Left','Callback',{@panleft,ax});
uicontrol('Style','pushbutton','Position',[100 40 40 20],'String','Right','Callback',{@panright,ax});
uicontrol('Style','pushbutton','Position',[60 60 40 20],'String','Up','Callback',{@panup,ax});
uicontrol('Style','pushbutton','Position',[60 20 40 20],'String','Down','Callback',{@pandown,ax});
end
% Callback functions
function panleft(obj,evt,ax)
dx = diff(ax.XLim);
ax.XLim = ax.XLim-0.05*dx;
end
function panright(obj,evt,ax)
dx = diff(ax.XLim);
ax.XLim = ax.XLim+0.05*dx;
end
function panup(obj,evt,ax)
dy = diff(ax.YLim);
ax.YLim = ax.YLim-0.05*dy;
end
function pandown(obj,evt,ax)
dy = diff(ax.YLim);
ax.YLim = ax.YLim+0.05*dy;
end
Does that help?
댓글 수: 4
Matthew Eicholtz
2017년 5월 5일
Copy and paste the entirety of my code into a new file, save, and run it. It should not give any error. Once you see how the buttons work, you can insert them in your code appropriately.
Matthew Eicholtz
2017년 5월 5일
I think you received that error because you copied the middle part of the code (that includes an "end" statement), but not the function header. Is that correct?
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!