如何在 Octave 中编写滑块以进行交互式绘图? [英] How to code a slider in Octave to have interactive plot?

查看:33
本文介绍了如何在 Octave 中编写滑块以进行交互式绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在外汇市场上绘制一个显示随机震荡指标的图表,为了验证哪个参数是设置它的最佳参数,我将使用滑块对其进行修改并在图表上显示更新的结果.

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 modify it and show updated result on plot.

我有我的历史数据,对于一个已定义的货币对(比如说 AUDUSD),在加载它之后,我计算随机震荡指标:

I have my historical data, for a defined pair (let say AUDUSD) and after loading it, I calculate Stocastic oscillator:

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

这是我绘制 stoch、fk 和 dk 时的图片:

This is a picture of what I have when I plot stoch, fk and dk:

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

I would add 3 sliders to the figure in order to change, in a range, parameters as input, so i.e. to have a slider that changes first parameter "n" between 3 and 50, "k" between 2 and 20, and "d" between 2 and 20.

我会使用 octave 的 UI 包,有人可以告诉我在我使用滑块时更新绘图吗?

I would use UI package in octave, can someone address me to have a plot updated when I use sliders?

弗朗西斯科

推荐答案

Andy 在评论中指出,我链接的示例在开箱即用的 octave 上不起作用;这是因为 Octave 暂时不喜欢某些事情的嵌套函数,所以我在下面复制了一个八度版本".

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

这篇关于如何在 Octave 中编写滑块以进行交互式绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆