GNU-Octave GUI Tools 

A slider, button and graphics axes communication in GNU-Octave GUI tools program is given below:

##Begin of callback plotting function

function myF()

   g = guidata(gcf);

   sv = get(g.ed,'value');

   plot(g.x, sv*sin(g.x),'parent',g.ax);

endfunction

##End of callback plotting function


##Begin of GUI and GUI Controls

f = figure('position',[150 150 500 500]);

h = get(f);

h.bn = uicontrol('style','pushbutton',

               'string', 'OK', 

               'position', [110 10 50 20],

               'callback',@myF);

h.ed = uicontrol('style', 'slider', 

               'position', [10 10 100 20]);

h.ax = axes('units','pixels',

          'position',[60,100,400,300]);

h.x = 0:0.1:10;

guidata(f,h);

##End of GUI and GUI Controls

The first lines code are callback function which is invoked each time when push button is pressed. 

function myF()

  g=guidata(gcf);

  sv = get(g.ed,'value');

  plot(g.x, sv*sin(g.x),'parent',g.ax);

endfunction

The function guidata reads graphics data, which contains the plotting points, x-points, current value of slider etc. The handler g provide this environment. Function get retrieves the value of slider value.  Now plot is plotted with given data.

f=figure('position',[150 150 500 500]);

This code line creates a new figure. The figure id is assigned to handler f.  This figure handler points to the figure properties which are stored in key-value format.  Before linking multiple GUI controls,  we first modify figure properties according to our use. Therefore, we get the list of properties assigned with the figure f.

h=get(f);

Now create a push button with call back function myF which is object pointed (@myF).   This button should not put children of figure but it should be member of key-value properties of the figure object.  To do this we use member creation property of handler h, i.e. handler of figure properties.

h.bn=uicontrol('style','pushbutton',

               'string', 'OK', 

               'position', [110 10 50 20],

               'callback',@myF);

Create a slider in similar manner as the push button is create. As the function callback is invoked only by the push button, hence slider control has no call back function.

h.ed=uicontrol('style', 'slider', 

               'position', [10 10 100 20]);

Crete axes in same level as the figure object is in the graphics hierarchy.

h.ax=axes('units','pixels',

          'position',[60,100,400,300]);

Create the x-points for plot of the function.

h.x = 0:0.1:10;

Now, copy these updated key-value properties of the figure object through handler h into the figure handler f.

guidata(f,h);

Now, when we set the slider and click on the push button, the call back function retrieves the required values from the figure properties' key-value arrangement. Using these values, plot is plotted. Each time we set slider value and click on the push button, call back function is called with updated values.  See the final output as shown in the following figure.

This program has no labels, slider is of small length and it has only one parameters. Now, above program is modified as given below:

##Begin of callback plotting function

function myF()

   g = guidata(gcf);

   am = get(g.am,'value');

   ph = get(g.ph,'value');

   plot(g.x, am*sin(g.x+ph),'parent',g.ax);

endfunction

##End of callback plotting function

##Begin of GUI and GUI Controls

f = figure('position',[150 150 500 500]);

h = get(f);


##Label for phase slider

uicontrol('style', 'text', 

                 'string', 'ph', 

                 'position', [10 20 20 20]);

h.ph = uicontrol('style', 'slider', 

                 'tag', 'Phase', 

                 'min', 0, 

                 'max', 3.14, 

                 'position', [40 20 450 20],

                 'callback',@myF);


##Label for amplitude slider

uicontrol('style', 'text', 

                 'string', 'A', 

                 'position', [10 50 20 20]);

h.am = uicontrol('style', 'slider', 

                 'tag', 'Amplitude', 

                 'min', 1, 

                 'max', 10, 

                 'position', [40 50 450 20],

                 'callback',@myF);

h.ax = axes('units','pixels',

            'position',[50,100,400,400]);

h.x = 0:0.1:10;

guidata(f,h);

##End of GUI and GUI Controls