MATLAB: Data “hides” when using plotyy

plotyy

I am using the following to plot two data sets ont he same graph
[AX,H1,H2] = plotyy(A,B,A,C,'plot');
It is in a loop. The first time I run this everything is fine, but if I run again, the second data set disappears after plotting the last point. It is visible upto this time.
set(H2,'Visible','on'); %# Make 2nd y axis visible, H2 as sometimes is disappears
I have used, but to no effect.

Best Answer

OK, I redid your callback function as follows for the LH plot. It produces same result for multiple clicks now altho needs a reset function to clear the data going forward and I also didn't worry about rescaling the dummy data to be within the axes ranges and all...
Take all the one-time stuff out of the loop and instead of making more line handles with the repetitive calling of plotyy simply update the data arrays and refresh. That's much faster and also far less overhead plus you were overwriting line handles with every call with the same variable when calling inside the loop.
See the documentation in the Graphics section on Animation for more details on how the guts works.
You also need a cleanup callback for the GUI; when you click the Close window X this currently errors as stuff is deleted in wrong order--I "know nuthink!" about GUIs so don't know precisely how that's supposed to be handled. I presume GUIDE will lead you through the process.
But, this should show enough of how to address the various handle graphics objects to stop the conflicting handles problems you're currently having. NB: as I said initially, the top-level outer axes handle is never referenced directly after the initial plotyy call that creates the two axes handles and in this technique it's the two line handles that are referenced instead of axes to update the data.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
lc=30;
sum=0;
%axes(handles.axes1)
i=1;
y1=2*i;
y2=3*i;
[hAx,H1,H2] = plotyy(handles.axes1,i,y1,i,y2);
ylabel(hAx(1),'FM', 'FontSize',8,'Color','red');
ylabel(hAx(2),'Mean','FontSize',8,'Color','blue');
xlabel(hAx(1),'Tile Number','FontSize',8)
set(hAx,'XLim',[0 lc],'FontSize',8);
set(hAx(2),'Xtick',[]);
set(hAx(1),'YColor','r')
set(hAx(2),'YColor','b')
grid on;
set(H1,'LineStyle','--', 'color', 'r','Marker','*','MarkerSize',4);
set(H2,'LineStyle','--', 'color', 'b','Marker','.');
hold(hAx(1),'on'), hold(hAx(2),'on')
xdat=i;
for i=2:lc
%Generate some dummy data
i;
xdat=[xdat; i];
y1=[y1;2*i];
y2=[y2;3*i];
set(H1,'xdata',xdat,'ydata',y1)
set(H2,'xdata',xdat,'ydata',y2)
drawnow
%Plot sum of i on axes2
sum=sum+i;
%axes(handles.axes2)
%hold on;

%plot(i,sum)
%hold off;
end
%h=handles.axes1;
%hold on;
h_legend=legend(hAx(1),'FM','Mean');
set(h_legend,'FontSize',7);
%drawnow;
hL=plot(hAx(1),[20 20], [0 150],'color','k','linewidth',2);