Saturday, October 24, 2015

Show Matlab figures professionally

  1. This Matlab script specifies the title Result image of the figure window, maximize the figure window, and makes the image look nice when saved to file.

    myFig = figure('Name','Result image','Units','normalized','Outerposition',[0 0 1 1],'PaperPositionMode','auto');
     
  2. This command fits the high-resolution image img on screen and suppresses Warning: Image is too big to fit on screen; displaying at 67%

    imshow(img,'InitialMagnification','fit')

    This command also suppresses the gray border, used when the figure has no title.

    imshow(img,'Border','tight','InitialMagnification','fit')
     
  3. This command shows the title Fig. 1. Distance of the figure, and allows to use symbols such as \hat{x_i}. Adjust FontSize to make complicated formulas in the title clearer.

    title('Fig. 1. Distance $d(\mathbf{x_i},\mathbf{\hat{x_i}})$','Interpreter','latex','FontSize',12.5);
     
  4. Use the following command in the command window of Matlab, to update figures without figure windows auto pop-up. Remember to change c:\yourpath to your project folder, and myscript to the name of your script (without suffix .m).

    !start /B matlab -automation -r "cd('c:\yourpath'); myscript; quit"

    There are other approaches proposed by MathWorks Support Team. However, some of them don't work on my Matlab R2014a, and docking figures makes the images too small when saved to file. Thus, I resort to start another Matlab session. Instead of use this command, you could just run another Matlab window though.
     
  5. This Matlab command saves figure to file.

    saveas(myFig, 'myImg.png')

    According to the official document, the saveas function uses a resolution of 150 DPI.

    If you'd like to change the resolution, use print. The following example sets the resolution to 200 dpi.

    print('-dpng','-r200','myImg.png')

    The following commands produce similar result. The resolution is the same as you see on the figure window.

    myFrame = getframe(myFig);

    mySnapshot = frame2im(myFrame);
    imwrite(mySnapshot, 'myImg.png')
     
  6. Download subplot_tight and suptitle from File Exchange to make subfigures look nicer.

References

No comments:

Post a Comment