'How to get rid of annoying extra white spaces on the margins when exporting surface plots to pdf?

How to get rid of annoying extra white spaces on the margins when exporting surface plots to pdf, in spite of the use of exportgraphics already? I guess the extra white spaces are caused by the use of view([-25 50]), which changes the default viewpoint, but this line is required for my application.

Example code:

[X,Y,Z] = peaks;
surf(X, Y, Z);
view([-25 50]);  %%% This line is necessary, but seems resulting in extra white spaces on the margins
exportgraphics(gcf, 'mypdf.pdf');

The resultant pdf file containing extra white spaces on the margin: enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source