Save a plot to a graphics format file

조회 수: 1 (최근 30일)
Julián Francisco
Julián Francisco 2011년 11월 16일
댓글: Image Analyst 2018년 3월 25일
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
Anil Kumar
Anil Kumar 2018년 3월 25일
Thank you norhan Mohamed..I could solve my issue with your code
Image Analyst
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2011년 11월 16일
Ah yes, finally our daily "how do I save a figure?" question. Please refer to the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_save_my_figure.2C_axes.2C_or_image.3F_I.27m_having_trouble_with_the_built_in_MATLAB_functions. where you'll see reference to the most downloaded File Exchange submission, export_fig(). Just use that and simplify your life. Braving it alone with low level MATLAB code is not for the faint of heart or novices, and often causes problems as you now have seen.
  댓글 수: 1
Julián Francisco
Julián Francisco 2011년 11월 16일
@Image Analyst: Thank you so much for your answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ben
Ben 2011년 11월 16일
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....
  댓글 수: 1
Julián Francisco
Julián Francisco 2011년 11월 16일
@Ben: Thank you for your answer. However, I have tried it adding
print(hplot,'-djpeg',FileName);
to my code but it does not work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by