MATLAB Answers

Show the value of uislider

9 views (last 30 days)
Jo
Jo on 13 Jun 2021
Edited: Adam Danz on 25 Jul 2021
I have question about how to show the value of the slider on the top. When I search for the answer, i saw others use callback and label... and other ways. But none of these work. The follow is the code i see on the documentation. Please let me know how to assign sld.value to label and show in the figure! Thanks a lot.
this is the error message
Error in slidervalue>@(sld,event)sliderMoving(event,cg,lbl) (line 10)
'ValueChangingFcn',@(sld,event) sliderMoving(event,cg,lbl));
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 386)
Error while evaluating Slider PrivateValueChangingFcn.
function slidervalue
% Create figure window and components
fig = uifigure('Position',[100 100 350 275]);
cg = uigauge('Parent',fig,'Position',[100 100 120 120]);
sld = uislider('Parent',fig,...
'Position',[100 75 120 3],...
'ValueChangingFcn',@(sld,event) sliderMoving(event,cg,lbl));
lbl = uilabel('Parent',fig,'Position',[145 180 80 120]);
end
% Create ValueChangedFcn callback
function sliderMoving(event,cg)
cg.Value = event.Value;
lbl.Value = event.Value;
end

Accepted Answer

Adam Danz
Adam Danz on 13 Jun 2021
Edited: Adam Danz on 13 Jun 2021
> i saw others use callback and label... and other ways. But none of these work.
Well, they do work, that's what people use those methods. I think what you mean is that you couldn't get them to work. Also, you're receiving an error when you run this code. Always include the entire error message when you're asking others to help fix the problem. The error message contains a lot of useful information.
There were several problems with the code.
  1. the uilabel was produced after the uislider callback function was assigned so the lbl handle was not generated yet (that produced the error message "Unrecognized function or variable 'lbl'."
  2. the lbl input was missing in the slideMoving function.
  3. You were assigning the slider value to lbl.Value. The uilabel does not have a Value property. You must assign the value as text which brings us to #4....
  4. The slider value must be converted to a string or character vector.
Here's the corrections:
% function slidervalue
% Create figure window and components
fig = uifigure('Position',[100 100 350 275]);
cg = uigauge('Parent',fig,'Position',[100 100 120 120]);
lbl = uilabel('Parent',fig,'Position',[145 180 80 120]); % <----- #1
sld = uislider('Parent',fig,...
'Position',[100 75 120 3],...
'ValueChangingFcn',@(sld,event) sliderMoving(event,cg,lbl));
% Create ValueChangedFcn callback
function sliderMoving(event,cg,lbl) % <----- #2
cg.Value = event.Value;
lbl.Text = string(event.Value); % <----- #3 & #4
end
  2 Comments
Adam Danz
Adam Danz on 14 Jun 2021
Glad I could help.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by