在 MATLAB 中使用连续滑块的值



我有点被困在这里。我试图阅读和实现一些简单的连续滑块脚本(比如这个),但我一无所获。

我只想做的是,当我滑动滑块时,在我的绘图中使用连续滑块值。但是,我无法弄清楚如何提取滑块的值来执行此操作。

例如,制作一个连续滑块,然后使用它来改变矢量的振幅,比如说,当你连续滑动它时。怎么能做到这一点呢?

谢谢。

像这样的东西?

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 "one_answers" 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 - 你找不到它并不奇怪 - 它没有记录在案。我从Yair Altman的网站上知道这一点。

从 Matlab 2014a 开始,您可以使用:

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

其中@slider是要定义的回调函数。工作整洁。

在回调中,您可以简单地使用:

slider_value=get(handle,'value');

(来源)

若要从滑块中提取值,请使用滑块手柄的get方法,如下所示,

sliderValue = get(hSlider,'Value')

相关内容

  • 没有找到相关文章

最新更新