matlab GUI保存axes(坐标轴)上的图像

1.默认方式

matlab GUI默认菜单的保存图像默认为保持全部GUI,包括使用" 菜单->编辑->复制图形"。

2 保存可见区域

2.1 代码

[FileName,PathName] = uiputfile({'*.jpg','JPEG(*.jpg)';...
                                             '*.bmp','Bitmap(*.bmp)';...
                                             '*.gif','GIF(*.gif)';...
                                             '*.*',  'All Files (*.*)'},...
                                             'Save Picture','Untitled');
if FileName==0
      disp('保存失败');
      return;
else
      h=getframe(picture);%picture是GUI界面绘图的坐标系句柄
      imwrite(h.cdata,[PathName,FileName]);
end           

2.2 说明

函数getframe()是为了获取坐标轴中的一帧图像,其返回的对象中有cdata和colormap两个成员。

2.3 效果

保存可见区域

3 保存带坐标轴的区域

3.1代码

new_f_handle=figure('visible','off');
new_axes=copyobj(picture,new_f_handle); %picture是GUI界面绘图的坐标系句柄
set(new_axes,'units','default','position','default');
[filename,pathname,fileindex]=uiputfile({'*.jpg';'*.bmp'},'save picture as');
if ~filename
     return
else
      file=strcat(pathname,filename);
      switch fileindex %根据不同的选择保存为不同的类型
      case 1
                  print(new_f_handle,'-djpeg',file);
      case 2
                  print(new_f_handle,'-dbmp',file);
      end
end
delete(new_f_handle);

3.2 说明

实际上是新建一个新的坐标图形,将GUI复制到新的图像上,输出新的图像,最后删除新建的图形句柄。

3.3 效果

保存带坐标轴的区域

参考文献

猜你喜欢

转载自www.cnblogs.com/jingshikongming/p/8971922.html
今日推荐