MATLAB Answers

Plot disappears after axes command

72 views (last 30 days)
I have a GUI where i plot two curves in the same figure. later in the program i want to plot a third curve in the same figure. To do so i first choose that figure to plot in with the axes command. When i do so one of the previous curves disappears and i can't understand why. Someone who knows?

  7 Comments

Show 4 older comments
Joakim Magnusson
Joakim Magnusson on 27 Jul 2016
Yes i have several axes and I have handles to all of them. Ok, then i think i have misunderstood what the axes command does. But i can't find any other way to do it.
First i choose and plot the axes from my GUI like this:
%The AXES command selects the set of axes to plot in,
axes(handles.viewCAM_handles.axes_posVelMA);
% Calculate startvalues for graph
MAPos = handles.varViewMAPosCAM(1);
velV = handles.varViewSLVelCAM(1);
posV = handles.varViewSLPosCAM(1);
% Calculate graph vectors
MAPos = [MAPos handles.varViewMAPosCAM(1:end-1)];
velV = [velV handles.varViewSLVelCAM(1:end-1)];
posV = [posV handles.varViewSLPosCAM(1:end-1)];
% Plot
[AX, h1, h2] = plotyy(MAPos, posV, MAPos, velV, 'plot', 'stairs');
And then in another callback for a pushbutton, I want to do this (the values have changed so I am not plotting the same thing again):
%The AXES command selects the set of axes to plot in,
axes(handles.viewCAM_handles.axes_posVelMA);
% Calculate graph vectors
MAPos = graphData.MAPos;
velV = graphData.velV;
posV = graphData.posV;
% Plot
[AX, h1, h2] = plotyy(MAPos, posV, MAPos, velV, 'plot', 'stairs');
But as i said, when i do the axes command the second time one curve I plotted earlier disappears. Now i understand that axes just creates an axes, so what should i use instead?
Adam
Adam on 27 Jul 2016
Are you using R2016a or later? If so the help appears to now say that plotyy is "not recommended", although it doesn't say why. It says you should use 'yyaxis' instead, which is a newer function.
Stephen Cobeldick
Stephen Cobeldick on 27 Jul 2016
Actually the axes documentation does describe one syntax which lets you provide an axes handle and it will make those axes the current axes: " axes(h) makes existing axes h the current axes and brings the figure containing it into focus"
However I would advise against using this in serious code, and instead always explicitly specify the axes in every plot function:
plotyy(axh,...)
Almost all plot functions support this, and it is ultimately much simpler and much more robust. It prevents users' clicking from changing the behavior of your code!

Sign in to comment.

Accepted Answer

Joakim Magnusson
Joakim Magnusson on 27 Jul 2016
The command:
set(gcf, 'currentaxes', handles.viewCAM_handles.axes_posVelMA)
did the trick. Now I have trouble with wrong y-axis values but that's another problem.

  2 Comments

Adam
Adam on 27 Jul 2016
plotyy, in common with all main plotting functions (possibly some older ones in toolboxes are exceptions, I'm not sure), has an overload that takes an explicit axes handle as its first argument.
I would strongly recommend to always use this form rather than trying to set which axes is in focus and then plotting with that as an assumption. Often that works fine, but if, for example, the user is able to click on a UI or bring a different figure into focus while the code is running this can lead to unexpected results.
Joakim Magnusson
Joakim Magnusson on 27 Jul 2016
Nice! I didn't know that, thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!