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

后端 未结 2 1006
自闭症患者
自闭症患者 2020-12-17 06:33

my target is to have a plot that shows Stochastic oscillator on forex market, and in order to validate which parameter is the best one to setup it, I would use a slider to m

2条回答
  •  庸人自扰
    2020-12-17 06:57

    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
    

提交回复
热议问题