Multiple sliders in interactive octave plot

I am trying to create a GUI where multiple variables could be modified using sliders. This is my example this far: I'm trying to alter the gradient of a linear function by summing two sliders.

I am completely new to the gui of octave and matlab, so I assume there is some fundamental error.

%%%%%% 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, 100, 101)
  x = t;
  y = 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', -100,                         ...
         'max', 100,                        ...
         'value', 0,                      ...
         'callback', {@plotstuff}          ...
       );
  %% Add ui 'slider' element      
  kslider = uicontrol (                    ...
         'style', 'slider',                ...
         'Units', 'normalized',            ...
         'position', [0.1, 0, 0.8, 0.1], ...
         'min', -100,                         ...
         'max', 100,                        ...
         'value', 0,                      ...
         'callback', {@plotstuff}          ...
       );
end

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

This is my error message:

㎫ >> error: operator *: nonconformant arguments (op1 is 0x0, op2 is 1x101) error: called from octave_test>plotstuff at line 43 column 5 error: operator *: nonconformant arguments (op1 is 0x0, op2 is 1x101) error: called from octave_test>plotstuff at line 43 column 5

This is the figure window:

enter image description here

1 answer

  • answered 2021-07-27 12:23 Cem

    function plotstuff (h, k, event)
    

    This signature is wrong. First argument to the callback function is a handle to a slider object. Second argument is the event. There is no third argument. This is the correct form:

    function plotstuff(h, event)
    

    In this callback, h equals either hslider or kslider (the numbers returned by uicontrol).

    You have multiple options here. You can make hslider and kslider global, as you did with t, and use them to get the slider values in the callback:

    %% in myplot
    global hslider kslider
    ...
    
    %% in plotstuff
    global hslider kslider
    ...
    n = get (hslider, 'value');
    m = get (kslider, 'value');
    

    If you don't want to use global variables, you can pass the two handles as additional arguments to the callback:

    %% in myplot
    hslider = uicontrol(...)
    kslider = uicontrol(...)
    
    set(hslider, 'callback', {@plotstuff, hslider, kslider})
    set(kslider, 'callback', {@plotstuff, hslider, kslider})
    

    Then you can use these arguments in plotstuff:

    function plotstuff (handle, event, hslider, kslider)
        n = get(hslider, 'value');
        m = get(kslider, 'value');
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum