Save a plot to a graphics format file
古いコメントを表示
I am designed the following GUI where there are an axes. I want to save the plot drawn inside them to a graphics format file. However, the file obtained is an image of the overall figure window. This is my code:
X = 0:pi/100:2*pi;
Y = sin(X);
fh = figure;
Pan1 = uipanel(fh,'Units','normalized','Position',[0 0 0.5 1],'title',...
'Panel1');
Pan2 = uipanel(fh,'Units','normalized','Position',[0.5 0 0.5 1],'title',...
'Panel2');
haxes = axes('Parent',Pan2,'Units', 'normalized','Position',...
[0.25 0.25 0.5 0.5]);
hplot = plot(haxes,X,Y);
FileName = uiputfile('*.bmp;*.png;*.jpg;*.tif','Save as');
saveas(hplot,FileName);
3 件のコメント
norhan Mohamed
2014 年 12 月 18 日
編集済み: norhan Mohamed
2014 年 12 月 18 日
Although,it's about 3 or 4 years along this thread,but I faced the same problem ,and I have found the solution,So I will put it here may it will help any one ,one day
axes(handles.axes3);
A = getframe(gca);
[filename,pathname]=uiputfile({'*.png';'*.jpg'},'Save Image as','C:\untitled.png');
name=fullfile(pathname,filename);
imwrite(A.cdata,name);
%check for the existence of the file and displays a message about the result of the file selection operation.
if isequal(filename,0) || isequal(pathname,0)
return;
Anil Kumar
2018 年 3 月 25 日
Thank you norhan Mohamed..I could solve my issue with your code
Image Analyst
2018 年 3 月 25 日
I should have warned of this earlier. Beware of using norhan's code. It only works if there is no scaling/zooming/fitting of the image to the axes. To illustrate that, just run this code:
rgbImage = imread('C:\Program Files\MATLAB\R2018a\toolbox\images\imdata\concordaerial.png');
imshow(rgbImage);
size(rgbImage)
A = getframe(gca)
You'll see
Warning: Image is too big to fit on screen; displaying at 33%
> In images.internal.initSize (line 71)
In imshow (line 336)
In test5 (line 10)
ans =
2036 3060 3
A =
struct with fields:
cdata: [679×1020×3 uint8]
colormap: []
Note how the size goes from 2036x3060 to 679x1020?
So A.cdata is not the same size as the original image!
It is the displayed size.
採用された回答
その他の回答 (1 件)
Ben
2011 年 11 月 16 日
2 投票
Most easy way is to make a print:
print( h, '-djpeg', 'test.jpg');
It will print the current view of the plot to your workspace....
カテゴリ
ヘルプ センター および File Exchange で Image Arithmetic についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!