matlab保存图片的四种方法
1 从菜单保存 <<回目录
可以保存为fig,eps,jpeg,gif,png,bmp等格式。
2 复制粘贴 <<回目录
edit------〉copy figure,再粘贴到其他程序,如word
3 saveas函数 <<回目录
saveas(gca,filename,fileformat)
不过此函数不好用常常出错
4 print函数 <<回目录
>> x=-pi:2*pi/300:pi;
>> y=sin(x);
>> plot(x,y);
>> print(gcf,'-dpng','abc.png')   % 保存为png格式的图片。
>> pwd

D:\Matlab\work

>> dir

getsavefilename>> % 现在到 D:\Matlab\work 应该能到图片 abc.png 了

>> figure(2)            % 新建一个句柄为2的图形窗口。
>> plot(x,cos(x));    % 在句柄为2的图形窗口上画图。
>> grid
>> print(2,'-djpeg','D:\abc.jpeg'); %将句柄为2的图形保存为jpeg/jpg格式的图片,
>>                                  %文件名为'D:\abc.jpeg'。
4.2 用法:print(图形句柄,存储格式,文件名); <<回目录
图形句柄,如果图形窗口标题栏是“Figure 3”,则句柄就是3.用gcf可以获取当前窗口句柄。
指定存储格式。常用的有:
png格式:‚-dpng‘ (推荐这一种,与bmp格式一样清晰,文件也不大)
jpeg: ‚-djpeg‘(文件小,较清晰)
tiff: ‚-dtiff‘
bmp: ‚-dbitmap‘(清晰,文件极大)
gif: ‚-dgif‘(文件小但不清晰)
文件名
在matlab中自动保存plot图像的程序
Step 1. 先使所画的图最大化,即占满整个屏幕
scrsz = get(0,'ScreenSize');
figure1=figure('Position',[0 30 scrsz(3) scrsz(4)-95]);或者(下面这种情况会把windows系统下面的任务栏也保存到图片中,不太好)scrsz = get(0,'ScreenSize');
figure1=figure('Position',[0 0 scrsz(3) scrsz(4)-66]);
% Step 2. 生成数据并画图
x=rand(100,1);
plot(x);
saveas(gcf,'filename','bmp');
saveas(gcf,'filename','emf');
saveas(gcf,'filename','jpg');
% Step 3. 清理现场
clear all; clc; close all;下面的代码可以创建一个大小为整个屏幕的四分之一,位置在屏幕在左上角的一个figure对像,使用root对象的ScreenSize属性来取得屏幕的尺寸,ScreenSize是由四个元素组成的数据:[left,bottom,width,height])。
scrsz = get(0,'ScreenSize');
figure2=figure('Position',[1 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2])
附:saveas
saveasSave figure or model using specified format
Syntax
saveas(h,'')
saveas(h,'filename','format')Descriptionsaveas(h,'') saves the figure or model with the handle h to the The format of the file is determined by the extension, ext. Allowable values for ext arelisted in this table.
ext ValueFormataiAdobe Illustrator `88bmpWindows bitmapemfEnhanced metafileepsEPS Level1figMATLAB figure (invalid for Simulink models)jpgJPEG image (invalid for Simulink models)mMATLAB M-file (invalid for Simulink models)pbmPortable bitmappcxPaintbrush 24-bitpgmPortableGraymappngPortable Network GraphicsppmPortable PixmaptifTIFF image, compressed
saveas(h,'filename','format') saves the figure or model with the handle h to the file calledfilename using the specified format. The filename can have an extension, but the extension isnot used to define the file format. If no extension is specified, the standard extensioncorresponding to the specified format is automatically appended to the filename.
Allowable values for format are the extensions in the table above and the device typessupported by print. The print device types include the formats listed in the table ofextensions above as well as additional file formats. Use an extension from the table above orfrom the list of device types supported by print. When using the print device type to specifyformat for saveas, do not use the prefixed -d.
Remarks
You can use open to open files saved using saveas with an m or fig extension. Other formatsare not supported by open. The Save As dialog box you access from the figure window's Filemenu uses saveas, limiting the file extensions to m and fig. The Export dialog box you accessfrom the figure window's File menu uses saveas with the format argument.
ExamplesExample 1: Specify File ExtensionSave the current figure that you annotated using the Plot Editor to a file named pred_preyusing the MATLAB fig format. This allows you to open the file pred_prey.fig at a later timeand continue editing it with the Plot Editor.
saveas(gcf,'pred_prey.fig')Example 2: Specify File Format but No ExtensionSave the current figure, using Adobe Illustrator format, to the file logo. Use the aiextension from the above table to specify the format. The file created is logo.ai.
saveas(gcf,'logo', 'ai')This is the same as using the Adobe Illustrator format from the print devices table, which is-dill; use doc print or help print to see the table for print device types. The file createdis logo.ai. MATLAB automatically appends the ai extension for an Illustrator format filebecause no extension was specified.
saveas(gcf,'logo', 'ill')Example 3: Specify File Format and ExtensionSave the current figure to the file star.eps using the Level 2 Color PostScript format. If youuse doc print or help print, you can see from the table for print device types that the devicetype for this format is -dpsc2. The file created is star.eps.
saveas(gcf,'star.eps', 'psc2')In another example, save the current model to the file trans.tiff using the TIFF format withno compression. From the table for print device types, you can see that the device type forthis format is -dtiffn. The file created is trans.tiff.
saveas(gcf,'trans.tiff', 'tiffn')See Also
hgsave, open, print
Printing for related functions
在matlab中自动保存plot图像的程序
Step 1. 先使所画的图最大化,即占满整个屏幕
scrsz = get(0,'ScreenSize');
figure1=figure('Position',[0 30 scrsz(3) scrsz(4)-95]);或者(下面这种情况会把windows系统下面的任务栏也保存到图片中,不太好)scrsz = get(0,'ScreenSize');
figure1=figure('Position',[0 0 scrsz(3) scrsz(4)-66]);
% Step 2. 生成数据并画图
x=rand(100,1);
plot(x);
saveas(gcf,'filename','bmp');
saveas(gcf,'filename','emf');
saveas(gcf,'filename','jpg');
% Step 3. 清理现场
clear all; clc; close all;下面的代码可以创建一个大小为整个屏幕的四分之一,位置在屏幕在左上角的一个figure对像,使用root对象的ScreenSize属性来取得屏幕的尺寸,ScreenSize是由四个元素组成的数据:[left,bottom,width,height])。
scrsz = get(0,'ScreenSize');
figure2=figure('Position',[1 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2])
附:saveas
saveasSave figure or model using specified format
Syntax
saveas(h,'')
saveas(h,'filename','format')Descriptionsaveas(h,'') saves the figure or model with the handle h to the The format of the file is determined by the extension, ext. Allowable values for ext arelisted in this table.
ext ValueFormataiAdobe Illustrator `88bmpWindows bitmapemfEnhanced metafileepsEPS Level1figMATLAB figure (invalid for Simulink models)jpgJPEG image (invalid for Simulink models)mMATLAB M-file (invalid for Simulink models)pbmPortable bitmappcxPaintbrush 24-bitpgmPortableGraymappngPortable Network GraphicsppmPortable PixmaptifTIFF image, compressed
saveas(h,'filename','format') saves the figure or model with the handle h to the file calledfilename using the specified format. The filename can have an extension, but the extension isnot used to define the file format. If no extension is specified, the standard extensioncorresponding to the specified format is automatically appended to the filename.
Allowable values for format are the extensions in the table above and the device typessup
ported by print. The print device types include the formats listed in the table ofextensions above as well as additional file formats. Use an extension from the table above orfrom the list of device types supported by print. When using the print device type to specifyformat for saveas, do not use the prefixed -d.
Remarks
You can use open to open files saved using saveas with an m or fig extension. Other formatsare not supported by open. The Save As dialog box you access from the figure window's Filemenu uses saveas, limiting the file extensions to m and fig. The Export dialog box you accessfrom the figure window's File menu uses saveas with the format argument.
ExamplesExample 1: Specify File ExtensionSave the current figure that you annotated using the Plot Editor to a file named pred_preyusing the MATLAB fig format. This allows you to open the file pred_prey.fig at a later timeand continue editing it with the Plot Editor.
saveas(gcf,'pred_prey.fig')Example 2: Specify File Format but No ExtensionSave the current figure, using Adobe Illustrator format, to the file logo. Use the aiextension from the
above table to specify the format. The file created is logo.ai.
saveas(gcf,'logo', 'ai')This is the same as using the Adobe Illustrator format from the print devices table, which is-dill; use doc print or help print to see the table for print device types. The file createdis logo.ai. MATLAB automatically appends the ai extension for an Illustrator format filebecause no extension was specified.
saveas(gcf,'logo', 'ill')Example 3: Specify File Format and ExtensionSave the current figure to the file star.eps using the Level 2 Color PostScript format. If youuse doc print or help print, you can see from the table for print device types that the devicetype for this format is -dpsc2. The file created is star.eps.
saveas(gcf,'star.eps', 'psc2')In another example, save the current model to the file trans.tiff using the TIFF format withno compression. From the table for print device types, you can see that the device type forthis format is -dtiffn. The file created is trans.tiff.
saveas(gcf,'trans.tiff', 'tiffn')See Also
hgsave, open, print
Printing for related functions

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。