7

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.

Community
  • 1
  • 1
Spacey
  • 2,941
  • 10
  • 47
  • 63

3 Answers3

10

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.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • 1
    Good answer. I may have missed the point of the question. I got the impression he was able to setup the listener, but didn't know how to get the `'Value'`. :) Anyway, +1 -- I'm gonna bookmark this `sliderDemo` for when I want to use it myself. – chappjc Oct 24 '13 at 06:15
  • Holy bejezus what type of black magic is this!? Fantastic. Yes. This is exactly what I had in mind. Teach me your ways oh great one! :P – Spacey Oct 24 '13 at 06:19
  • @Learnaholic: thanks, more than willing to :) But as I mentioned, most of the kudos should go to [Yair](http://stackoverflow.com/users/233829/yair-altman) -- *he* figured this one out :) – Rody Oldenhuis Oct 24 '13 at 06:23
  • @chappjc: Thanks! And +1 to you to; it's the only valid answer within the limits of documented MATLAB. – Rody Oldenhuis Oct 24 '13 at 06:25
  • So I am a little confused in studying your code. 1) Who is calling 'sliderCallBack' exactly? It is in the uicontrol, and in the listener... 2) Why do you have (~,~) as inputs into sliderCallBack why not just not put anything there? 3) Maybe just a brief summary of 'how' it is working? I use MATLAB alot, but this is my first foray into GUI stuff... thanks!! – Spacey Oct 24 '13 at 06:25
  • 1
    @Learnaholic: **1)** Both the slider (when clicking the arrows (small step), or clicking on the slider bar (large step)) and the listener (continuous slides) are calling the callback. **2)** The double-tilde is because I don't use the arguments passed by default to the callback, which are the slider handle, and an event structure. – Rody Oldenhuis Oct 24 '13 at 06:28
  • Thanks you once again for your explanations, this is truly remarkable. :-) – Spacey Oct 24 '13 at 14:57
  • Rody, I have tried to extend your solution, to use two sliders instead of one. The first one for amplitude as you have, the second for say, phase. However it does not seem to be that straight-forward... perhaps I am missing something? Thanks in advance! – Spacey Oct 25 '13 at 04:06
  • @Learnaholic: You have to assign both `handle.listener`s to their own variable in MATLAB. Those variable will be unused (supress with `%#ok`), but this prevents the second call to `handle` to overwrite the `ans` variable, thus destroying the first listener. See also my edit. – Rody Oldenhuis Oct 25 '13 at 05:26
  • @RodyOldenhuis I appear to have succeeded extending this based on your code. [Take a look](https://dl.dropboxusercontent.com/u/4724281/sliderDemoV2.m). – Spacey Oct 25 '13 at 05:33
  • 1
    @Learnaholic: starting to look real nice :) – Rody Oldenhuis Oct 25 '13 at 05:36
2

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)

mmumboss
  • 690
  • 1
  • 5
  • 13
1

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

sliderValue = get(hSlider,'Value')
chappjc
  • 30,359
  • 6
  • 75
  • 132