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

13,517

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
Share:
13,517
Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&amp;A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on June 14, 2022

Comments

  • Tim
    Tim almost 2 years

    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!

  • Tim
    Tim over 14 years
    The problem is that using saveas() or print() does not preserve the saved image size same as the plot.
  • Amro
    Amro over 14 years
    wasn't that already addressed in a previous question of yours: stackoverflow.com/questions/1848176/…
  • Tim
    Tim over 14 years
    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
    Tim over 14 years
    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?
  • Amro
    Amro over 14 years
    1) this is only to prevent a bug og getting an empty plot in the saved file: mathworks.com/support/solutions/en/data/1-15HNG/index.html 2) 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..