Show the value of uislider

29 次查看(过去 30 天)
Jo
Jo 2021-6-13
编辑: Adam Danz 2021-7-25
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

采纳的回答

Adam Danz
Adam Danz 2021-6-13
编辑:Adam Danz 2021-6-13
> 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 个评论
Jo
Jo 2021-6-14
Thanks a lot for your help! I'll include the error message next time!
Adam Danz
Adam Danz 2021-6-14
编辑:Adam Danz 2021-7-25
Glad I could help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by