【问题标题】:Use the value of continuous slider in MATLAB在 MATLAB 中使用连续滑块的值
【发布时间】:2026-01-27 18:20:15
【问题描述】:

我有点卡在这里。我曾尝试阅读并实现一些简单的连续滑块脚本 (like this one),但我一无所获。

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

例如,制作一个连续的滑块,然后用它来改变矢量的幅度,比如说,当您连续滑动它时。怎么办?

谢谢。

【问题讨论】:

    标签: matlab user-interface event-handling slider handle


    【解决方案1】:

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

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

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

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

    slider_value=get(handle,'value');
    

    (Source)

    【讨论】:

      【解决方案2】:

      这样的?

      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 - 找不到它并不奇怪 - 它没有记录在案。我从Yair Altman's site 知道这一点。

      【讨论】:

      • 好答案。我可能错过了问题的重点。我的印象是他能够设置监听器,但不知道如何获得'Value'。 :) 无论如何,+1 - 我会将此sliderDemo 加入书签,以供我自己使用时使用。
      • 天哪,这是什么黑魔法!?极好的。是的。这正是我的想法。教我你的方法哦,太好了! :P
      • @Learnaholic:谢谢,非常愿意 :) 但正如我所提到的,大部分荣誉应该归于Yair -- he 想出了这个 :)
      • @Learnaholic: 1) 滑块(单击箭头(小步)或单击滑块(大步)时)和听者(连续滑动) 正在调用回调。 2) 双波浪号是因为我没有使用默认传递给回调的参数,即滑块句柄和事件结构。
      • @Learnaholic:开始看起来很不错:)
      【解决方案3】:

      要从滑块中提取值,使用滑块句柄的get方法如下,

      sliderValue = get(hSlider,'Value')
      

      【讨论】: