MATLAB Answers

Display Value of a Slider in a text box without GUIDE

2 views (last 30 days)
Laurent on 8 Jan 2020
Commented: Laurent on 8 Jan 2020
Im struggling to find informations about how to display Data on a GUI without GUIDE.
More specificaly I have a slider 0->1 that I use to set my Threshold value (Image processing). I want the User to be able to see which value the slider is currently set to and changes brought to it.
There is no error Code but the value isnt showing up/ getting updated (Stays at 0)
These are fragments of my Code, please tell me if you need to see the whole thing:
fig = figure('Visible', 'on','Position',[0,0,1715,895], ...
'Name', 'Projekt Matlab Workshop','Numbertitle','off', 'MenuBar', 'none')
%... GUI Elements
slider = uicontrol('Style','slider','Min',0,'Max',1,'Value',0.5,...
'SliderStep',[0.025 0.05],'Position',[685,205,350,35]);
% ... Labels
lbl_schwellwertAnzeige = uicontrol('Style','text','String',num2str(get(slider, 'Value')),...
'Position',[1040,150,10,30], 'FontSize', 11);


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 8 Jan 2020
Edited: Stephen Cobeldick on 8 Jan 2020
If you want the slider to update something (text, plot, anything) as it moves, then you will need to add a listener:
Here is a complete working solution for pre-R2014b MATLAB, that updates an edit box while dragging the slider:
ht = uicontrol('style','edit','Position',[10,60,40,40]);
hs = uicontrol('style','slider','Position',[10,10,400,20]);
fun = @(o,e)set(ht,'String',num2str(get(e,'NewValue')));
addlistener(hs, 'Value', 'PostSet',fun);
And it looks like this:
For post R2014b use this::
fun = @(~,e)set(ht,'String',num2str(get(e.AffectedObject,'Value')));
See also:


Show 1 older comment
Stephen Cobeldick
Stephen Cobeldick on 8 Jan 2020
".. what the "@ (o,e)" does"
It defines an anonymous function (called "lambda function" in some other languages):
Instead of saving a function in a file I just created a simple anonymous function to make the MWE self-contained and compact. You can make the listener call any function you want.
"The edit box is showing up on my GUI but there is no value to be seen:"
The edit box does not have a value when it is created, although it would be trivial to add. As the example stands you have to move the slider to trigger the listener which calls the callback which sets that text. No movement -> no text.
There is nothing stopping you from putting some default text into your textbox when you create it.
Rik on 8 Jan 2020
Two side-notes: MWE is a Minimal Working Example, and o and e are shorthands in this case for the object handle and event object.
Laurent on 8 Jan 2020
Thanks for the explanations.
That works for me

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by