Editor's Note: This file was a File Exchange Pick of the Week
An indispensible uicontrol: linked slider, text, and labels all self-contained in a panel.
FUNCTION:
sliderHandle,panelHandle,editHandle] = sliderPanel(parent, PanelPVs, SliderPVs, EditPVs, LabelPVs, numFormat)
Creates a slider in a separate uipanel, with an associated interactive EditBox, and left and right labels showing the minimum and maximum values of the slider, respectively.
Moving the slider automatically updates the textbox, and vice versa. Both slider movement and text edits will trigger (non-recursively) the callback of the slider.
The EditBox automatically disallows the entry of non-numeric values, or of values outside of [min,max]. Attempts to enter disallowed values will be ignored.
Two syntaxes are supported. One gives FULL control over all elements of the sliderPanel, and a second provides easier access to a subset of the functionality.
UPDATE: Right-clicking the slider now resets the sliderPanel to its default (creation) value.
Brett Shoelson (2021). sliderPanel (https://www.mathworks.com/matlabcentral/fileexchange/13845-sliderpanel), MATLAB Central File Exchange. Retrieved .
Inspired: Dynamical System Viewer, cqi_plotmatrix( data,varargin ), SegmentTool: An Interactive GUI for Segmenting Images
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Here are some more extensive tests and comments on the code:
----
figure
[s,p,e]=sliderPanel(gcf,...
{'title','Threshold','pos',[0.1 0.2 0.8 0.15],'fontweight','b','units','pixels'},...
{'max',80,'value',60,'callback','disp(''Slid'')'},...
{},...
{{'string','Low','foregroundcolor','b'},...
{'string','High','foregroundcolor','r'},...
{'fontweight','b'}},...
'%0.1f');
get(e,{'string','value','userdata'}), get(s,'Value')
ans =
'60' [0] '60'
ans =
60
%in your code you have a bug fix on line 393 that sets the editbox value, but there isn't a similar line earlier upon initialization, say after line 339 where the edithandle userdata is set
%Move the slider by hand to 25.3
get(e,{'string','value','userdata'}), get(s,'Value')
ans =
'25.3' [0] '25.3'
ans =
25.3134
%editbox value is not set inside the updateText() function
-----
So it looks to me like the code could use a few more lines to keep the value in sync with the other two. But I was also wrong in how I thought it worked. I had assumed that there was more linkage than there is - that if I changed the slider value programmatically there was some internal callback that would align the editbox with it and vice versa. In thinking about it more, I have no idea how one would do this in matlab if changing it programmatically would not trigger an event (although if it is possible, it would be nice to know how). For my purposes, I'll just be sure to change the slider value and all three editbox settings together when I manipulate one programmatically.
Thanks for the widget, though. I never use sliders anymore without it.
Programmatically changing the editbox or slider values gives me the following behaviors:
[s,p,e]=sliderPanel(...);
set(s,'Value',20);
% this changes the value of the slider to 20 and also moves the slider. but neither the editbox value nor display changes.
set(e,'Value',70);
%this changes the value of the editbox to 70, but the editbox display does not change and neither does the slider position of value.
i got it Brett, too basic it was..
hi Brett,can i specify property as "orientation"? i mean if i want vertical slider then which property will help me?
Thanks.
Since most of the sliders I've ever generated also had exactly these uicontrols attached to them, this makes great sense. Thanks for posting it.
I tested it out in both R2006b and in R14SP1 (both on a Mac.) It worked nicely in R2006b, but in the older release, the slider bar would not move properly, and when the figure was resized, the slider bar moved to a different place in the figure. So there appear to be definite problems in older releases.