Matlab繪圖基礎——用print函式批量儲存圖片到檔案(Print figure or save to file)
Matlab繪圖基礎——用print函式批量儲存圖片到檔案(Print figure or save to file)
2.2. Batch Processing(圖片儲存“批處理”)filename
一、用法解析
print(figure_handle,'formats','-rnumber','filename')
%將圖形儲存為formats格式,解析度為600的(預設為72),最好指定的解析度大一點,否則儲存圖形的效果較差.
1.1. 解析度-rnumber
Use -rnumber to specify the resolution of the generated output.
To set the resolution(解析度) of the output file for a built-in MATLAB format, use the -r switch.
l For example, -r300 sets the output resolution to
The –r switch is also supported for Windows Enhanced Metafiles(增強型圖元檔案), JPEG, TIFF and PNG files, but is not supported for Ghostscript raster formats(柵格檔案).
l For more information, see Printing and Exporting without a DisplayandResolution Considerations
—— 來自matlab幫助“print”頁中
1.2. 輸出圖片的“格式”formats
請參考本文末尾
二、用法示例
% Save the figure with the handle h to a PostScript file named Figure1, which can be printed later.
H = figure; % 指定圖片列印figure_handles
% 若沒有figure_handle,則預設print當前顯示圖片
plot(1:4,5:8)
print(h,'-dpng','-r200','Figure1') % 這三行程式碼就夠用了
2.1. 設定輸出圖片的“影象縱橫比”
When you set the axes Position to [0 0 1 1] so that it fills the entire figure, the aspect ratio is not preserved when you print because MATLAB printing software adjusts the figure size when printing according to the figure's PaperPositionproperty. To preserve the image aspect ratio(影象縱橫比) when printing, set the figure's 'PaperPositionMode' to 'auto' from the command line.
set(gcf,'PaperPositionMode','auto')
% Setting the current figure's (gcf) PaperPositionMode to auto enables
% you to resize the figure window and print it at the size you see on the screen.
舉例:
surf(peaks);shading interp % 畫圖,shading使影象美觀
axis off % 不顯示座標軸
set(gcf,'PaperPositionMode','auto'); % 設定影象縱橫比
print('-dpng','-r200','a'); % 儲存圖片,名為a
——來自matlab幫助 Printing Images
2.2. Batch Processing(圖片儲存“批處理”)filename
You can use the function form of print to pass variables containing file names. For example, this for loop uses file names stored in a cell array to create a series of graphs and prints each one with a different file name:
fnames = {'file1', 'file2', 'file3'};
for k=1:length(fnames)
surf(peaks);shading interp
print('-dtiff','-r600',fnames{k}) % fnames is a cell of string arrays so each element is a string
end
————來自matlab幫助“print”頁末
注:如果你不能調整輸出解析度和檔案格式,可能是"Printing and Exporting without a Display"問題,具體檢視幫助“print”頁
———分割線————————————————————————————
1.2. 輸出圖片的“格式”formats
The following table shows the supported output formats for exporting from figures and the switch settings to use. In some cases, a format is available both as a MATLAB output filter and as a Ghostscript output filter. All formats except for EMF are supported on both Windows and UNIX platforms.
Bitmap or Vector(向量) |
Print Command Option String |
MATLAB or Ghostscript |
|
BMP monochrome BMP |
Bitmap |
Ghostscript |
|
BMP 24-bit BMP |
Bitmap |
Ghostscript |
|
BMP 8-bit (256-color) BMP (this format uses a fixed colormap) |
Bitmap |
Ghostscript |
|
BMP 24-bit |
Bitmap |
MATLAB |
|
EMF |
Vector |
MATLAB |
|
EPS black and white |
Vector |
MATLAB |
|
EPS color |
Vector |
MATLAB |
|
EPS Level 2 black and white |
Vector |
MATLAB |
|
EPS Level 2 color |
Vector |
MATLAB |
|
HDF 24-bit |
Bitmap |
MATLAB |
|
ILL (Adobe Illustrator) |
Vector |
MATLAB |
|
JPEG 24-bit |
Bitmap |
MATLAB |
|
PBM (plain format) 1-bit |
Bitmap |
Ghostscript |
|
PBM (raw format) 1-bit |
Bitmap |
Ghostscript |
|
PCX 1-bit |
Bitmap |
Ghostscript |
|
PCX 24-bit color PCX file format, three 8-bit planes |
Bitmap |
Ghostscript |
|
PCX 8-bit newer color PCX file format (256-color) |
Bitmap |
Ghostscript |
|
PCX Older color PCX file format (EGA/VGA, 16-color) |
Bitmap |
Ghostscript |
|
PDF Color PDF file format |
Vector |
Ghostscript |
|
PGM Portable Graymap (plain format) |
Bitmap |
Ghostscript |
|
PGM Portable Graymap (raw format) |
Bitmap |
Ghostscript |
|
PNG 24-bit |
Bitmap |
MATLAB |
|
PPM Portable Pixmap (plain format) |
Bitmap |
Ghostscript |
|
PPM Portable Pixmap (raw format) |
Bitmap |
Ghostscript |
|
SVG Scalable Vector Graphics (For Simulink Models Only) |
Vector |
MATLAB |
|
TIFF 24-bit |
Bitmap |
MATLAB |
|
TIFF preview for EPS files |
Bitmap |