How to update Value using Slider

2 vues (au cours des 30 derniers jours)
Hussam Ibrahim
Hussam Ibrahim le 20 Juil 2016
Here is my function:
function sliderDemo
f = figure(1);
%// Some simple to plot function (with tuneable parameter)
x = 1;
%// Make initial plot
p = line([x(A),x(A)], ylim());
axis tight
%// re-position the axes to make room for the slider
set(gca, 'position', [0.1 0.25 0.85 0.7]);
%// initialize the slider
h = uicontrol('parent', f,'units', 'normalized','style', 'slider','position', [0.05 0.05 0.9 0.05],'min', 1,'max', 10,'value', x,'callback', @sliderCallback); %// This is called when using the arrows
%// and/or when clicking the slider bar
hLstn = handle.listener(h,'ActionEvent',@sliderCallback); %#ok<NASGU>
function sliderCallback(~,~)
delete(p);
value = x(get(h,'value'));
disp(value)
p = line([value,value], ylim());
axis tight
end
end
When I execute it, it is giving me the following error:
No constructor 'handle.listener' with matching signature found.
Error in sliderDemo (line 21)
hLstn = handle.listener(h,'ActionEvent',@sliderCallback); %#ok<NASGU>
Subscript indices must either be real positive integers or logicals.
Error in sliderDemo/sliderCallback (line 24)
value = x(get(h,'value'));
Error while evaluating UIControl Callback
The way I want to be setup, is have a line drawn everytime I switch value on slider.
I want to use this for image thresholding
Can someone please be of an assist?

Réponses (1)

Walter Roberson
Walter Roberson le 20 Juil 2016
Use addlistener() instead of handle.listener
In your code
value = x(get(h,'value'));
you should not expect that the slider value will be constrained to integers, so you should not be using the value as an index into x. You could consider using the value as the location to interp1() x .
In your code
p = line([x(A),x(A)], ylim());
but A is not apparently defined. Is A a function?
In your code
value = x(get(h,'value'));
you have set the slider min to 1 and max to 10 so even if you got an integer value you would be expecting values larger than 1. But you defined
x = 1;
so x is a scalar, not a vector.

Community Treasure Hunt

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

Start Hunting!

Translated by