How to code a slider in Octave to have interactive plot?

安稳与你 提交于 2019-12-03 20:28:50

Andy pointed out in the comments that the example I linked to doesn't work on octave out of the box; this is because Octave doesn't like nested functions for certain things for the time being, so I've reproduced an 'octave version' below.

%%%%%% In file myplot.m %%%%%
function myplot

  %% Create initial figure and spiral plot
  figure;  axes ('position', [0.1, 0.3, 0.8, 0.6]);
  global t;   t = linspace (0, 8*pi, 100);
  x = t .* cos(t);  y = t .* sin(t);
  plot (x, y);  axis ([-100, 100, -100, 100]);

  %% Add ui 'slider' element      
  hslider = uicontrol (                    ...
         'style', 'slider',                ...
         'Units', 'normalized',            ...
         'position', [0.1, 0.1, 0.8, 0.1], ...
         'min', 1,                         ...
         'max', 50,                        ...
         'value', 10,                      ...
         'callback', {@plotstuff}          ...
       );
end

%% Callback function called by slider event
%% Also in file myplot.m (i.e. a subfunction)
function plotstuff (h, event)
  global t;
  n = get (h, 'value');
  x = n * t .* cos(t);  y = n * t .* sin(t);
  plot (x, y);  axis ([-100, 100, -100, 100]);
end

Have a look at this demo which will give you an window like that which should answer all your questions:

The relevant parts for your specific questions are:

h.noise_slider = uicontrol ("style", "slider",
                            "units", "normalized",
                            "string", "slider",
                            "callback", @update_plot,
                            "value", 0.4,
                            "position", [0.05 0.25 0.35 0.06]);
....
 noise = get (h.noise_slider, "value");

Be sure to use the Qt toolkit!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!