Show the value of uislider

25 visualizaciones (últimos 30 días)
Jo
Jo el 13 de Jun. de 2021
Editada: Adam Danz el 25 de Jul. de 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

Respuesta aceptada

Adam Danz
Adam Danz el 13 de Jun. de 2021
Editada: Adam Danz el 13 de Jun. de 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 comentarios
Jo
Jo el 14 de Jun. de 2021
Thanks a lot for your help! I'll include the error message next time!
Adam Danz
Adam Danz el 14 de Jun. de 2021
Editada: Adam Danz el 25 de Jul. de 2021
Glad I could help.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by