MATLAB Answers


How do I replace the 'EraseMode' property in MATLAB R2014b?

In MATLAB R2014a and earlier versions, I could animate my plots using the 'EraseMode' property. I noticed that the property does not exist in MATLAB R2014b. How do I replace the 'EraseMode' property?




1 Answer

Answer by MathWorks Support Team on 2 Aug 2016
 Accepted Answer

Starting in R2014b, the 'EraseMode' property has been removed from all graphics objects. You can still achieve most of the effects produced by 'EraseMode', such as creating animations or producing overlaid colors, using the techniques described here.
To accumulate a picture by adding data to each frame, use one of these approaches instead of setting the EraseMode property to 'none':
  • Use 'hold on' to retain the current data and add new data to the graph.
  •  Use the new 'animatedline' function to create line animations.
  • Use the 'movie' function to play recorded movie frames.
For example, create a line animation using the new 'animatedline' function.
theta = linspace(0,2*pi,10000);
h = animatedline();
for t = theta
    drawnow update;
For more information on creating line animations, refer to the documentation for animatedline
To immediately display changes to object data, call 'drawnow update' or 'drawnow expose' instead of setting EraseMode to 'xor'.
  • 'drawnow update' updates a graph with maximum loop speed, but only sends updates if the renderer is free. If the renderer is busy, then 'drawnow update' discards the updates.
  • 'drawnow expose' updates a graph as quickly as possible without losing any updates.
For more information, refer to the documentation for drawnow.
For example, change the 'YData' for a line and display the updates.
t = linspace(0,2*pi,10000);
y = exp(sin(t));
h = plot(t,y);
for k = 1:0.01:10
   y = exp(sin(t.*k));
   h.YData = y;
   drawnow expose
To produce overlaid colors, use transparency instead of setting Erasemode to 'xor'.
p1 = patch([0,2,2,0],[0,0,2,2],[1,1,1,1]);
p2 = patch([1,3,3,1],[1,1,3,3],[2,2,2,2]);
p2.FaceAlpha = 0.5;
In R2014a and earlier, setting the 'EraseMode' property to 'xor' increases the rendering speed. Remove code that sets the 'EraseMode' property to get similar rendering speeds.


I previously used xor mode to draw crosshairs (positions updated via dragging or clicking mouse) that were clearly visible on a variety of images and matrices under a variety of custom colormaps. Is there a substitute in 2014b for this use case?
Drawing two lines on top of each other (say, white and black) with the top line using a '--' linestyle is a poor replacement, since the images are blocky and the hatched style often becomes camouflaged and difficult to see.
Please contact technical support and describe your use case. Ideally include a small code example which shows what used to work in previous releases and no longer works with R2014b.
Here is a code sample demonstrating what is lost when 'EraseMode','xor' is not available:
h=axes('XLim',[0,2],'YLim',[0,2]); patch([0,1,1,0],[0,0,2,2],'r'); patch([1,2,2,1],[0,0,2,2],'g'); l=line([0,2],[0,2],'EraseMode','xor'); l1=line([0,2],[2,0],'Color','r');
Of course you can run this snippet only in an earlier Matlab version. In this example, the second line, drawn without 'xor', is visible only in one of the patches. When you have to draw a line on top of an arbitrarily colored image, where you are going to cross a variety of colors, XOR is the only way to ensure that the line is visible (almost) everywhere.
I will accept not being able to accurately print figures, but I still need to be able to SHOW the line on top of a raster in interactive session

Sign in to comment.