如何保存MATLAB GUI界面中的图片

如何保存MATLAB GUI界面中的图片

[email protected] 有任何问题,接受反馈。

代码来源自网络,作者未知,侵删。

原生支持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

猜你喜欢

转载自blog.csdn.net/qq_36408085/article/details/82952875