Why plot handles in GUI disappear?

Hi, I'm building a GUI with 2 axes and plotting 8 plots on each one of them. On the 1st axes when I get the handles of the second plot the first handle disappears (and so on, so at the end of the loop I have only the 8th plot handle) On the 2nd axes this problem doesn't happen and at the end of this loop I have all the plots handles. My function is happening in the create function of the GUI. This is my code:
function firstPlot(handles)
%%harvesting
plotHandlesHarv = zeros(2,8);
axesHandlesHarv = [handles.LeftAxes,handles.RightAxes];
GraphCheck=[handles.AngCheck handles.SweepCheck handles.HarvCheck handles.VinCheck...
handles.VoutCheck handles.KCheck handles.IrefCheck handles.StepCheck];
set(GraphCheck,'value',1) %pick all the graph lines
set(GraphCheck(8),'value',0); % step graph check is 0
for i = 1:2
plotHandlesHarv(i,1)= plot(axesHandlesHarv(i),0,'-k','LineWidth',2); %ang
hold on
plotHandlesHarv(i,2)= plot(axesHandlesHarv(i),0,'--k','LineWidth',2); %sweep
plotHandlesHarv(i,3)= plot(axesHandlesHarv(i),0,'-r','LineWidth',2); %harvest torque
plotHandlesHarv(i,4)= plot(axesHandlesHarv(i),0,'-b','LineWidth',2); %Vin
plotHandlesHarv(i,5)= plot(axesHandlesHarv(i),0,'-g','LineWidth',2); %Vout
plotHandlesHarv(i,6)= plot(axesHandlesHarv(i),0,'-c','LineWidth',2); %K
plotHandlesHarv(i,7)= plot(axesHandlesHarv(i),0,'--m','LineWidth',2); %Iref
plotHandlesHarv(i,8)= plot(axesHandlesHarv(i),0,'--r','LineWidth',2); %step
hold off
set(axesHandlesHarv(i),'YGrid','on');
set(axesHandlesHarv(i), 'XTickLabelMode', 'Manual');
set(axesHandlesHarv(i), 'XTick', [])
end
Thanks!

7 comentarios

Nir - please clarify what you mean by the handles disappearing. Your code is calling plot eight times but is only ever changing the colour and perhaps line style
plot(axesHandlesHarv(i),0,'--k','LineWidth',2);
and is always just passing in zero. Is this an example only? Because if not, then it makes sense that you would only see the eighth plot as it would be above the seven that preceded it.
Nir Raviv
Nir Raviv el 2 de Sept. de 2016
To clarify my question- after this section, when I try to set the Ydata in real time on the plots at the LeftAxes, I get a warning that the plot handle was delete. I've debugged my code and saw that the handle deletes right after I plot the second line (this problem appears only for the LeftAxes!)
Walter Roberson
Walter Roberson el 3 de Sept. de 2016
Reminder, the default for plotting is no marker. You are plotting a single point each time, and never specify a marker, so none of your plots is going to produce anything visible.
Nir Raviv
Nir Raviv el 15 de Sept. de 2016
I have tried to put an array instead of one point and specified the specs of the 8 lines but the problem remains still. I can't reach the plot handles when I try to set new Ydata in real time. this is the new initial code:
function firstPlot(handles)
temp = 0:0.004:0.9960;
xdata=repmat(temp',1,8);
ydata=zeros(250,8);
plotHandlesHarv = zeros(2,8);
axesHandlesHarv = [handles.LeftAxes,handles.RightAxes];
GraphCheck=[handles.AngCheck handles.SweepCheck handles.HarvCheck handles.VinCheck...
handles.VoutCheck handles.KCheck handles.IrefCheck handles.StepCheck];
set(GraphCheck,'value',1) %pick all the graph lines
set(GraphCheck(8),'value',0); % step graph check is 0
Color=['-k','--k','-r','-b','-g','-c','--m','--r']';
for i = 1:2
for j=1:8
plotHandlesHarv(i,j)= plot(axesHandlesHarv(i),xdata(:,j),ydata(:,j),Color(j),'LineWidth',2);
end
set(axesHandlesHarv(i),'YGrid','on');
set(axesHandlesHarv(i), 'XTickLabelMode', 'Manual');
set(axesHandlesHarv(i), 'XTick', []);
end
Adam
Adam el 15 de Sept. de 2016
Not sure if this is the cause of the problem, especially since your revised code does not use 'hold' so I'll just add it as a comment:
'hold' also has function syntax that allows you to specify the axes handle. Always do this when you are using multiple axes (I always do it anyway unless coding on command line) - i.e.
hold( axesHandlesHarv(i), 'on' )
Also, which version of Matlab are you using? If you are using post R2014b I would not recommend initialising plotHandlesHarv to zeros. It appears to still work fine, but graphics objects are now classes in their own right and initialising the array to zeros casts them back into the old-style graphics handles which are just doubles with magic hidden properties when you call 'get' on them.
Walter Roberson
Walter Roberson el 15 de Sept. de 2016
Which MATLAB version are you using? If you are using R2014b or later, then you should be initializing plotHandlesHarv with gobjects() instead of zeros()
Nir Raviv
Nir Raviv el 15 de Sept. de 2016
Thanks Adam and Walter. Both your answers combined helped me solve this problem

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 31 de Ag. de 2016

Comentada:

el 15 de Sept. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by