如何保存MATLAB GUI界面中的圖片
csuzhangyang@gmail.com 有任何問題,接受反饋。
代碼來源自網絡,作者未知,侵刪。
原生支持png,bmp,jpg格式。另外自行添加了清晰度比較高的eps格式(薦)。
新建一個按鈕,按鈕的代碼如下。
其中第二行的new_axes=copyobj(handles.axes1,new_f_handle)的handles.axes1意思是保存的是axes1中的圖,以此類推。
function pushbutton19_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton19 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 以下內容拷貝到按鈕函數下。
new_f_handle=figure('visible','off'); %新建一個不可見的figure
new_axes=copyobj(handles.axes1,new_f_handle); %axes1是GUI界面內要保存圖線的Tag,將其copy到不可見的figure中
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);%將圖線縮放
[filename pathname fileindex]=uiputfile({'*.png';'*.bmp';'*.jpg';'*.eps';},'圖片保存為');
if filename~=0%未點“取消”按鈕或未關閉
file=strcat(pathname,filename);
switch fileindex %根據不同的選擇保存為不同的類型
case 1
print(new_f_handle,'-dpng',file);% print(new_f_handle,'-dpng',filename);效果一樣,將圖像打印到指定文件中
fprintf('>>已保存到:%s\n',file);
case 2
print(new_f_handle,'-dbmp',file);
fprintf('>>已保存到:%s\n',file);
case 3
print(new_f_handle,'-djpg',file);
fprintf('>>已保存到:%s\n',file);
case 4
print(new_f_handle,'-depsc',file);
fprintf('>>已保存到:%s\n',file);
end
msgbox(' 圖線已成功保存!','完成!');
end