MATLAB: Do the PRINTand SAVEAS commands from MATLAB take a screen capture instead of the figure when I export the MATLAB figure to a bitmap (BMP) file

bmpcaptureexportfigureMATLABprintsaveasscreenscreenshot

Why do the "print" and "saveas" commands from MATLAB take a screen capture instead of the figure when I export my MATLAB figure to a bitmap (BMP) file?
I am trying to export my MATLAB figure to a BMP file using the "saveas" and "print" commands. When I minimize the figure and activate another window (such as the task manager), the exported BMP file contains the screen capture, including the task manager, instead of only the MATLAB figure window. How can I export the MATLAB figure to a BMP file?

Best Answer

The above problem occurs because the "saveas" function uses the '-dbitmap' option when exporting a MATLAB figure to a BMP file, which takes a snapshot of the screen. This issue will also occur if the "print" function is used with the '-dbitmap' option. There are several methods you can use to avoid this behavior:
1. Leave the figure open (i.e., do not minimize the figure) and you will generate a BMP with the figure as you expect.
2. Use the "print" function with the '-dbmp' option:
print (gcf, '-dbmp', 'myfile.bmp')
3. Export the MATLAB figure to a JPEG file, and then convert to a BMP file using the following commands:
print -djpeg filename;
imwrite(imread([filename '.jpg']),[filename, '.bmp'], 'bmp')
4. Capture the figure frame and save it using "imwrite":
F = getframe(gcf);
imwrite(F.cdata,'myimage.bmp','bmp');