Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Matlab invisible plot under terminal as an image with same size

I am SSH connecting to a Linux server and do some MATLAB programming. I would like to save invisible plot as

figH = figure('visible','off') ;  
% Plot something  
% save the plot as an image with same size as the plot   
close(figH) ;   

saveas() and print() will change the size of the saved image different than the size of plot. Also for print(), all three renderer modes (-opengl, -ZBuffer and -painters) can not be used in terminal emulation mode on the Linux server. getframe() doesn't work either. I wonder how I can solve these problems? Thanks and regards!

like image 354
Tim Avatar asked Dec 05 '09 19:12

Tim


People also ask

Can you save a MATLAB plot as a JPEG image?

Save Plots InteractivelyThe export button supports three image formats (PNG, JPEG, and TIFF), as well as PDF files, which can contain images or vector graphics, depending on the content in the axes.

How do I save a figure window in MATLAB?

fig . savefig( H , filename ) saves the figures identified by the graphics array H to a FIG-file named filename . fig . savefig( H , filename , 'compact' ) saves the specified figures in a FIG-file that can be opened only in MATLAB® R2014b or later releases.

How do I save 300 dpi in MATLAB?

To save a figure as an image at a specific resolution, call the exportgraphics function, and specify the 'Resolution' name-value pair argument. By default, images are saved at 150 dots per inch (DPI). For example, create a bar chart and get the current figure. Then save the figure as a 300-DPI PNG file.


1 Answers

Use the following sequence of commands to connect and start MATLAB:

ssh -x user@server          # disabled X11 forwarding
unset DISPLAY               # unset DISPLAY variable
matlab -nodisplay           # start MATLAB without the desktop

then a simple plot to illustrate:

figure, close                    # must do this first, otherwise plot is empty
plot(1:10)                       # usual plotting
print file                       # save the figure as file.ps
saveas(gcf, 'file.eps', 'eps2c') # saveas aslo works
exit                             # done

I just tried it myself, and it works as expected.


EDIT:

You can always specify the DPI resolution using -r<number>, for example a very high resolution:

print -dpdf -r600 file.pdf

Note that you can use -r0 to specify screen resolution.

Also you can turn on WYSIWYG printing of figures using the PaperPositionMode property:

figure, close
plot(1:10)
set(gcf, 'PaperPositionMode', 'auto')
print -deps2c -r0 file.eps
exit
like image 64
Amro Avatar answered Oct 24 '22 13:10

Amro