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

如何保存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
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值