Plotyy Axis Problem

Hello everyone!

I have a specific problem with the plotyy function. I don’t quite understand what is happening but here is what I want and how I coded it, and then I explain what happens:

I have a uipanel and there is an axis with the handle hax. upon a buttonpress that is also on the panel, a calculation takes place and then something is plotted on this axes with plotyy.

The panel/programm is designed in such a way that a callback function is running at all times and multiple calculations should be able to be performed. Before each new calculation, the axis are cleared with the cla function.

p = uipanel;
hax = axes(p, "fontsize", 12, "ygrid", "on", "position", [0.53 0.06 0.15 0.25]);

h.start_button = uicontrol(p, "style", "pushbutton",... 
                           "string", "Start",... 
                           "units", "normalized",...
                           "fontsize", 15,...
                           "fontweight", "bold",...
                           "callback", @(h,e)callback_function(h,p,hax),...
                           "position", [0.71 0.655 0.265 0.06]);

function callback_function (obj, p, hax)  

switch(gcbo)

  case {h.start_button}

      ax_temp_get = getappdata(get(obj, 'Parent'), 'Axes');
      if isempty(ax_temp_get) == true      
        ax_temp = hax;
        cla(ax_temp);
      else
        ax_temp = ax_temp_get;
        cla(ax_temp(1));
        cla(ax_temp(2));
      endif;

  hax = ax_temp;

  [ax_temp] = calculation_and_plot (hax);

  setappdata(get(obj, 'Parent'), 'Axes', ax_temp);  

  endswitch  
endfunction

set(gcf, "color", get(0, "defaultuicontrolbackgroundcolor"))
guidata (gcf, h)
callback_function (gcf, true);

And the code of the calculation_and_plot function is a different .m file with

function [ax_temp] = calculation_and_plot (hax);

hax = hax(1);
[ax_temp, h1, h2] = plotyy(hax, x_data, [y_data1, y_data2]);

 
endfunction;

My plan with this is simply always when the start button is pressed, I want to clear the axis on the panel so that a new plot can be plottet there. the problem though is that after the first calculation the axis handle has two handles since plotyy gives 2 handles as output. therefore I implemented the setappdata and getappdata trick to grab the second axis handle if handle so that I can clear it. but the problem is, that after the first iteration the plot just does disappear on the panel competely. I am not sure how to circumvent this problem, has anybody an idea?

It looks like this is a side effect of an attempt to fix bug #44246 in newplot.m:

        if (isprop (ca, "__plotyy_axes__"))
          ## Hack for bug #44246.  There is no way to reset or remove a
          ## property created with addproperty short of deleting the object.
          delete (ca);
          ca = axes ();

We should probably save and restore (at least) the "Units" and "Position" properties to avoid the issue you are seeing.
Could you please open a bug report on savannah referencing this thread?
GNU Octave - Bugs: Browse Items [Savannah]

As a workaround, you could probably do something like this in your function:

switch(gcbo)

  case {h.start_button}

      ax_temp_get = getappdata(get(obj, 'Parent'), 'Axes');
      if isempty(ax_temp_get) == true      
        cla(hax);
      else
        old_units = get(ax_temp(1), 'Units');
        old_position = get(ax_temp(1), 'Position');
        delete(ax_temp(1));  % deleting one axes should also delete the other ones
        hax = axes('Units', old_units, 'Position', old_position);
      endif;

  [ax_temp] = calculation_and_plot (hax);

  setappdata(get(obj, 'Parent'), 'Axes', ax_temp);  

  endswitch  
endfunction

set(gcf, "color", get(0, "defaultuicontrolbackgroundcolor"))
guidata (gcf, h)
callback_function (gcf, true);

I pushed a change here:
octave: 4d2b528464ad (gnu.org)

Should be part of Octave 7 when it will be released.

1 Like