Using the print command makes disappearing a surface from a figure

1 view (last 30 days)
Hello,
I am using Matlab R2014b. I have a picture which is made by 3 items: 1) lines made with plot 2) cloud of points made by stem 3) surface made using surf
figure
plot(t,wg,'b')
hold on
stem3(D(:,1),D(:,2),zeros(N,1),'b.');
s1 = surf(XX,YY,PPDF2,'EdgeColor','none','LineStyle','none');
shading flat;alpha(s1,'color');
sub1=gca;
view(2)
saveas(gcf,[city'.fig'])
print('-dpng','-r600',[city,'.png'])
When I do the print command, the surface disappears. I googled and it seems to be related to the alpha .... I like the way the plot looks and I would like to export it at high res as png. I tried epsc and I get the same problem.
If you give me your email address, I will send you the fig image. Any suggestion is welcome :-)
Thanks a lot
Antonio

Accepted Answer

Matt Cohen
Matt Cohen on 8 Sep 2015

Hi Mortain,

I understand that you are encountering an issue where a surface does not appear in a PNG image when using the print command in MATLAB R2014b.

Since you did not provide the specific data used to create the figure in the code snippet, I created some dummy variables to try to reproduce the issue. The code I used is shown below:

N = 15;
t = linspace(0,1,N);
wg = linspace(0,1,N);
D = rand(N,2);
XX = rand(N);
YY = rand(N);
PPDF2 = rand(N);
city = 'boston';
figure
plot(t,wg,'b')
hold on
stem3(D(:,1),D(:,2),zeros(N,1),'b.');
s1 = surf(XX,YY,PPDF2,'EdgeColor','none','LineStyle','none');
shading flat;alpha(s1,'color');
sub1=gca;
view(2)
saveas(gcf,[city '.fig'])
print('-dpng','-r600',[city,'.png'])

I have also included the PNG output from the code snippet above:

The example code does not produce the same issue that you are experiencing. In the image, you can see the line running diagonally across the figure, the surface with the specified transparency visible throughout the figure, and various points from the stem plot on the right side of the figure.

This issue might be related to the renderer being used. The two renderer options are OpenGL and Painters. Each renderer displays and sorts objects in different ways. To check the current renderer, you can use the command "get(gcf,'renderer')" while the figure is open. It is likely using OpenGL for this. You could try setting the renderer to Painters ("set(gcf,'renderer','painters')") or including the '-painters' flag in the "print" command. Additionally, you could try entering the command "opengl software" before running the code and seeing if this possibly changes the output. To help further, though, I would need to be able to work with your figure. You can attach a .fig file to a question or comment in MATLAB Answers.

If none of this helps resolve your issue, you could consider using the function "export_fig", which can be found at the following link:

http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig

Note that this is a MATLAB File Exchange submission from a user, so it is not directly supported by MathWorks. However, it is a popular tool for exporting figures and has proven to be successful in creating a wide variety of exported images and files.

I hope this helps.

Matt Cohen

  1 Comment
mortain
mortain on 14 Sep 2015
Thanks Matt for your reply. I actually achieved this by using export_fig. Then, another day, I wrote a script where I opened the figure and instead of grabbing the last figure with gcf I just used the print command, and actually the png came out ok.
Thanks again Antonio

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 8 Sep 2015
print('-opengl', '-dpng','-r600',[city,'.png'])
  1 Comment
mortain
mortain on 14 Sep 2015
Thanks a lot for your reply Walter. I tried changing the driver and didn't work. I found another solution as explained above :-)

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!