how to extract slider's value

I'm working in MATLAB and want to obtain the value of a slider and use it as an input for calculations in another function. How can I retrieve the slider's value and pass it to another function? I attempted to use get(h,'value'), but encountered an error. How should I go about this?
Error encountered while using get:
Error using matlab.graphics.Graphics/get
Property value not found in class matlab.graphics.GraphicsPlaceholder, or is not present in all
elements of the array of class matlab.graphics.GraphicsPlaceholder.
Here's what I tried doing:
hslide(i) = uicontrol('style','slider','units','normalized');
value = get(hslide,'value');

 Accepted Answer

You are storing the slider object in an array. hslide is the array, and hslide(i) is the slider. When you try to get the value, you are getting the Value of each element of the array, but not all elements of the array are sliders (in particular some are GraphicsPlaceholders, maybe because you haven't created those sliders yet), which causes the error.
If you intend to store your slider in an array and just get the value of the ith slider in that array, then
hslide(i) = uicontrol('style','slider','units','normalized');
value = get(hslide(i),'value');
Or if you do not intend to store slider in an array, then:
hslide = uicontrol('style','slider','units','normalized');
value = get(hslide,'value');

2 Comments

it worked, thank you!
You're welcome!

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Asked:

on 23 Oct 2023

Commented:

on 24 Oct 2023

Community Treasure Hunt

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

Start Hunting!