how to save a graph in jpg or any other image format

조회 수: 362 (최근 30일)
Krishnendu Mukherjee
Krishnendu Mukherjee 2012년 3월 12일
답변: Toshia M 2023년 3월 17일
how to save a graph in jpg or any other image format

채택된 답변

Thomas
Thomas 2012년 3월 12일
You can do it programatically as:
figure1 = figure;
axes1 = axes('Parent',figure1)
hold(axes1,'all');
plot(plot what you want to plot)
saveas(figure1,'finename.ext') % here you save the figure
  댓글 수: 4
Pratik Nikam
Pratik Nikam 2020년 8월 10일
편집: Walter Roberson 2020년 8월 10일
I have plotted a curve fitted line of a curve in a graph. Now I wish to measure its angle with X axis. that's why I need a graph to be saved as an image, so that I could apply a formula and get its angle without manual interference. I use 2018a version, but the code you suggested doesn't seem working for it. Can you please help?
Here is a part of the code:
%r1 & c1 are taken from the image for the curvefitting.
%max(r1)= highest value of r1 & min(r1)= minimum value of r1
coefficients = polyfit(r1, c1, 3);
fittedA = linspace(min(r1), max(r1), 500);
fittedB = polyval(coefficients, fittedA);
plot(fittedA, fittedB, 'b-', 'linewidth', 1);
set(gca, 'YDir', 'reverse'); %mirroring the plot
Walter Roberson
Walter Roberson 2020년 8월 10일
I do not understand why you think that applying a formula to an image showing a graph is going to get you a useful result.
figure1 = figure;
axes1 = axes('Parent',figure1)
%r1 & c1 are taken from the image for the curvefitting.
%max(r1)= highest value of r1 & min(r1)= minimum value of r1
coefficients = polyfit(r1, c1, 3);
fittedA = linspace(min(r1), max(r1), 500);
fittedB = polyval(coefficients, fittedA);
plot(axes1, fittedA, fittedB, 'b-', 'linewidth', 1);
set(axes1, 'YDir', 'reverse'); %mirroring the plot
saveas(figure1, 'YourFileNname.png')
But this would get you an image of a plot complete with tick marks and tick labels and lots of whitespace. It is going to be a nuisance to process the image to get anything useful out of it. It would make a lot more sense to just process the fittedA and fittedB data directly.

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

추가 답변 (3개)

Jacob Halbrooks
Jacob Halbrooks 2012년 3월 12일
From your figure, select File->Save as and choose a file type in the dialog. If you need to save your figure programmatically, the PRINT command has options to choose a file type (such as the -djpeg flag for JPG format).

Toshia M
Toshia M 2023년 3월 17일
Starting in R2020a, you can use the exportgraphics function to save the contents of any axes, figure, tiled chart layout, or panel within a figure. This function allows you to save an image such as a JPEG, TIFF, or PNG. You can also save the content as vector graphics in a PDF file. For example, create a plot and save it as a JPEG. Then save it as vector graphics in a PDF file.
plot([0 4 2 6 3])
ax = gca;
exportgraphics(ax,"myplot.jpg") % Save a JPEG
exportgraphics(ax,"myplot.pdf","ContentType","vector") % Save a PDF

Image Analyst
Image Analyst 2012년 3월 12일

카테고리

Help CenterFile Exchange에서 Graphics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by