Question

I am kinda stuck here. I have tried to read and implement some simple continuous slider scripts, (like this one), but I am not getting anywhere.

What I simply want to go, is use the continuous slider value in my plot, as I slide the slider. However, I cannot figure out how to extract the value of the slider to do so.

So for example, make a continuous slider, and then use it to change the amplitude of a vector lets say, as you continuously slide it. How can that be done?

Thanks.

Was it helpful?

Solution

Something like this?

function sliderDemo

    f = figure(1);

    %// Some simple to plot function (with tuneable parameter)
    x = 0:0.1:2*pi;
    y = @(A) A*sin(x);

    %// Make initial plot
    A = 1;
    p = plot(x, y(A));
    axis tight
    axis([0 2*pi -10 10])

    %// 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',...    %// so yo don't have to f*ck with pixels
        'style'   , 'slider',...        
        'position', [0.05 0.05 0.9 0.05],...
        'min'     , 1,...               %// Make the A between 1...
        'max'     , 10,...              %// and 10, with initial value
        'value'   , A,...               %// as set above.
        'callback', @sliderCallback);   %// This is called when using the arrows
                                        %// and/or when clicking the slider bar


    %// THE MAGIC INGREDIENT
    %// ===========================

    hLstn = handle.listener(h,'ActionEvent',@sliderCallback); %#ok<NASGU>
    %// (variable appears unused, but not assigning it to anything means that 
    %// the listener is stored in the 'ans' variable. If "ans" is overwritten, 
    %// the listener goes out of scope and is thus destroyed, and thus, it no 
    %// longer works.

    %// ===========================


    %// The slider's callback:
    %//    1) clears the old plot
    %//    2) computes new values using the (continuously) updated slider values
    %//    3) re-draw the plot and re-set the axes settings
    function sliderCallback(~,~)
        delete(p);
        p = plot(x, y(get(h,'value')));
        axis tight
        axis([0 2*pi -10 10])
    end

end

PS - it's not strange that you couldn't find it -- it's not documented. I know this from Yair Altman's site.

OTHER TIPS

Since Matlab 2014a, you can use:

addlistener(h_slider,'ContinuousValueChange',@slider);

Where @slider is a Callback function to be defined. Works neat.

Within the callback, you can simply use:

slider_value=get(handle,'value');

(Source)

To extract the value from the slider, use the get method of the slider's handle as follows,

sliderValue = get(hSlider,'Value')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top