How do you enable a slider to scroll an entire panel?

51 views (last 30 days)
So far, I have only succeeded in scrolling the contents in the panel. However, I want the whole panel to scroll along with the content.
any form of help will be highly appreciated
Thanks in advance, Cordel Williams

Answers (3)

Walter Roberson
Walter Roberson on 12 Jun 2013
If you have contents of an axes (lines, surfaces, drawn text) that you want to move while the axes itself stays in one location, then use panning to adjust what is visible in the axes (by either using the pan tool, or by adjusting the axes XLim and YLim)
When you have a collection of objects that are to be moved within a figure while the figure itself stays in one place, such as the position of axes, or position of controls relative to the figure edge, then put the collection inside a uipanel and have slides adjust the location of the uipanel relative to the figure.
The above is enough in most cases, but if you need a uipanel itself to move, then put the uipanel within a uipanel. And if you need that to move, put it within a uipanel. And so on. It's uipanels all the way down!
Cordelle on 12 Jun 2013
the thing is that i used Guide to create the panels

Sign in to comment.

Selby on 6 Dec 2016
u=uipanel(g,'backgroundcolor',[1 0 1],'units','pixels','position',[1000 1000 g.InnerPosition(3)-19 1000]);
[jj hh]=javacomponent(j,[10 10 g.InnerPosition(3)-20 g.InnerPosition(4)-20],g);
%components to add to panel
'position',[0.1 0.1 100 100],'string','blah');
This uses Yair's findjobj

Jorrit M
Jorrit M on 8 Dec 2020
The above answers didn't work for me for a programmatically generated GUI in R2019b, and I struggled for an embarrasingly long time to get this stupid scrollbar to work, so I'll leave my solution here for future reference. Hopefully someone will find it useful.
My setup: I wish to create a panel with a variable number of lines and text edit boxes that the user can scroll through to edit. The main window handle is ptrMainGUI. I have two subpanels: one for the slider, and one for the content that it will scroll through: ptrPanelStimParamsParent and ptrPanelStimParams. intParams here is the number of lines I'm going to generate. As long as you create variables ptrMainGUI, as well as intParams, cellProps, cellVals, cellComments for your lines and values to generate in the panel I think it should work. Anyway, godspeed and good luck:
%get main GUI size and define subpanel size
ptrMainGUI.Units = 'pixels';
vecGuiSize = ptrMainGUI.Position;
ptrMainGUI.Units = 'normalized';
dblPanelHeight = 0.2;
dblPanelY = 0.1;
%calculate the total size of the subpanel content
dblTotSize = (intParams+1)*30;
dblRelSize = (dblTotSize/(vecGuiSize(end)*dblPanelHeight))+dblPanelHeight;
%create the panels
ptrPanelStimParamsParent = uipanel('Parent',sFigRE.ptrMainGUI);
set(ptrPanelStimParamsParent,'Position',[0.01 dblPanelY 0.94 dblPanelHeight]);
ptrPanelStimParams = uipanel('Parent',ptrPanelStimParamsParent);
set(ptrPanelStimParams,'Position',[0 0 1 dblRelSize]);
ptrSlider = uicontrol('Style','Slider','Parent',sFigRE.ptrMainGUI,...
'Units','normalized','Position',[0.94 dblPanelY 0.05 dblPanelHeight],...
%add all variables
vecParamTextPtrs = nan(1,intParams);
vecParamEditPtrs = nan(1,intParams);
for intParam=1:intParams
vecParamTextPtrs(intParam) = uicontrol(ptrPanelStimParams,'style','text',...
'Position',[1 (intParams*30)-((intParam-1)*30) 150 25],'String',cellProps{intParam},'FontSize',10);
vecParamEditPtrs(intParam) = uicontrol(ptrPanelStimParams,'style','edit',...
'Position',[150 (intParams*30)-((intParam-1)*30) 390 25],'String',cellVals{intParam},'Tooltip',cellComments{intParam},'FontSize',10);
%add callback
Now, to scroll properly we'll need to use the following function with some arcane transformations. The "vecSize(end)-1" is to avoid showing an empty block the size of exactly one parent panel:
function slider_callback1(hObject,eventdata,ptrSubPanel)
vecSize = ptrSubPanel.Position;
val = get(hObject,'Value');
dblRealMax = vecSize(end) - 1;
dblStartY = val*dblRealMax;
vecSetPanelPos = [0 -dblStartY 1 vecSize(end)];
set(ptrSubPanel,'Position',vecSetPanelPos);%[from-left from-bottom width height]

Community Treasure Hunt

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

Start Hunting!