tags:

views:

1019

answers:

1

Hi, 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!

+3  A: 

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

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
Amro
The problem is that using saveas() or print() does not preserve the saved image size same as the plot.
Tim
wasn't that already addressed in a previous question of yours: http://stackoverflow.com/questions/1848176/how-not-to-save-non-image-area-in-matlab-image-plot
Amro
The solution provided there is actually not for terminal mode and Matlab invisible plot (I accepted it only based on that it works on X mode and Matlab visible plot). Specifically getframe() will return null even under the way you suggested to connect to the server and run Matlab.
Tim
I updated my answer above. I assume by "not same size" you meant resolution..
Amro
Thanks Amro! That solves my problem! Just curious: (1) what is the purpose of "figure, close", and its difference with "figH = figure('visible','off')"? (2) what's the purpose of "unset DISPLAY", and the combination of "ssh -X" with it?
Tim
1) this is only to prevent a bug og getting an empty plot in the saved file: http://www.mathworks.com/support/solutions/en/data/1-15HNG/index.html2) I used those to show that your are not running in X mode. `ssh -x` (small x!) disables X11 forwarding, and then we clear the DISPLAY environment variable that specifies which device/host to use for graphical ouput. Usually just starting MATLAB with the `-nodisplay` option is sufficient..
Amro

related questions