Clear Filters
Clear Filters

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

315 views (last 30 days)
how to save a graph in jpg or any other image format

Accepted Answer

Thomas
Thomas on 12 Mar 2012
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 Comments
Pratik Nikam
Pratik Nikam on 10 Aug 2020
Edited: Walter Roberson on 10 Aug 2020
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 on 10 Aug 2020
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.

Sign in to comment.

More Answers (3)

Jacob Halbrooks
Jacob Halbrooks on 12 Mar 2012
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).

Image Analyst
Image Analyst on 12 Mar 2012

Toshia M
Toshia M on 17 Mar 2023
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

Categories

Find more on Graphics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!