如何在Octave中编码滑块以获得交互式绘图?

Fra*_*osi 2 user-interface plot slider octave stochastic

我的目标是在外汇市场上有一个显示随机振荡器的图,为了验证哪个参数是设置它的最佳参数,我会使用滑块修改它并在图上显示更新的结果.

我有我的历史数据,对于一个已定义的对(比如说是AUDUSD),加载后,我计算了Stocastic振子:

function [stoch, fk, dk] = stochastic(n, k, d)
    X=csvread("AUDUSD_2017.csv");
    C=X(2:length(X),5);
    L=X(2:length(X),4);
    H=X(2:length(X),3);
    O=X(2:length(X),2);
    for m=n:length(C)-n
        stoch(m)=((C(m)-min(L(m-n+1:m)))/(max(H(m-n+1:m))-min(L(m-n+1:m))))*100;

    endfor

for m=n:length(C)-n

    fk(m)=mean(stoch(m-d:m));

 endfor
for m=n:length(C)-n

    dk(m)=mean(fk(m-d:m));
endfor


endfunction
Run Code Online (Sandbox Code Playgroud)

这是我在绘制stoch,fk和dk时所拥有的图片:

绘图基于参数(14,7,7作为输入

我会在图中添加3个滑块,以便在一个范围内更改参数作为输入,即,使用滑块将第一个参数"n"更改为3到50之间,"k"更改为2到20之间,以及"d" "在2到20之间.

我会在octave中使用UI包,当我使用滑块时,有人可以告诉我更新的情节吗?

弗朗切斯科

And*_*ndy 5

看看这个演示,它将为您提供一个应该回答您所有问题的窗口:

演示uicontrol

您的具体问题的相关部分是:

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");
Run Code Online (Sandbox Code Playgroud)

一定要使用Qt工具包!


Tas*_*nou 5

安迪在评论中指出,我链接到的示例不适用于开箱即用的八度音程;这是因为 Octave 目前不喜欢某些事情的嵌套函数,所以我在下面复制了一个“octave 版本”。更新(2023-03-31):嵌套函数已经支持一段时间了:请参阅旧帖子下面的更新以获取示例

%%%%%% 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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更新 (2023-03-31)

不确定嵌套函数在什么时候得到了正确的支持,但至少从 Octave 8.1 开始是这样的。因此,过去在评论中链接的旧示例(但现在指向已删除的 matlab 问题)现在可以按预期工作。我将其复制在这里以供将来参考。使用嵌套函数而不是子函数很方便,因为这使您可以访问父函数的局部变量,从而消除了对“全局变量”黑客等的需要。

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

  %% Create initial figure and spiral plot
  figure;  axes ('position', [0.1, 0.3, 0.8, 0.6]);
  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}          ...
       );

  %% Callback function called by slider event
  function plotstuff (h, event)
    n = get (h, 'value');
    x = n * t .* cos(t);  y = n * t .* sin(t);
    plot (x, y);  axis ([-100, 100, -100, 100]);
  end
end
Run Code Online (Sandbox Code Playgroud)