MATLAB Answers

Show the value of uislider

조회 수: 9(최근 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
Adam Danz
Adam Danz 2021년 6월 14일
Glad I could help.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by