MATLAB: Problem with using printf when exporting a figure to png

export to pngfillpatchprint

I have notice the following problem in Matlab. Suppose that we want to generate two lines and two rectangles that overlap.
figure(1);
p1=plot([1 5],[2 7]);
hold on
p2=plot([1 5],[2 8],'r');
s1=fill([2 4 4 2],[3 3 4 4],[0.8 0.8 0.8]);
s2=fill([1 3 3 1],[2 2 3.5 3.5],[ 0.7 0.0 0.0]);
set(gca,'children',[p1 s2 s1 p2]);
set(gcf,'PaperPositionMode','auto')
set(gcf,'Units','centimeters','Position',[5 5 9 8])
print('-dpng','-r800','image1')
Everything looks fine in the window, that is the red rectangle is over the grey one, the blue line is at the top and the red one is at the bottom. However, when I export the figure to png format the faces are transparent, while the edges are located at the desired levels. Could anyone tell me what is wrong?

Best Answer

Finally, I have solved the problem. Just use the rectangle() function instead of patch() or fill() or area().
figure(1);
p1=plot([1 5],[2 7]);
hold on
p2=plot([1 5],[2 8],'r');
s1=rectangle('Position',[2 3 2 1],'Curvature',[0.0 0.0],'FaceColor',[0.8 0.8 0.8]);
s2=rectangle('Position',[1 2 2 1.5],'Curvature',[0.0 0.0],'FaceColor',[0.7 0.0 0.0]);
set(gca,'children',[p1 s2 s1 p2]);
set(gcf,'PaperPositionMode','auto');
set(gcf,'Units','centimeters','Position',[5 5 9 8]);
print('-dpng','-r800','image1');
Probably, there is some bug in Matlab when exporting overlapping fill (area, patch) regions to file.