Save a .FIG as .JPG (syntax error?)

I currently have a string varible that updates my fig title and file names.
savefig(sprintf('%s - F vs V.fig', NAME))
But I also want to save a .JPG of the file too. I've tried:
saveas(sprintf('%s - F vs V', NAME),'.jpg')
Is there a syntax error I'm overlooking?

 Accepted Answer

To save a jpg with saveas() you have to give a proper figure handle (at least gcf) as first argument and change '.jpg' to 'jpeg' as format specifier (no dot and including the "e").
e.g.:
clearvars, close all
clc
NAME = 'myFig'; % figure name and prefix of jpeg file name
fig = figure('Name', NAME); % creates figure and returns figure handle
plot(rand(100,1)) % plot some random data
saveas(fig, sprintf('%s - F vs V', NAME),'jpeg') % save as jpeg
However, i would recommend to use a vector graphics format or png instead of jpeg, because jpeg could contain ugly artifacts.

3 Comments

Stephen23
Stephen23 on 19 May 2020
Edited: Stephen23 on 19 May 2020
+1 clear advice, good recommendation to use PNG.
Of course a complete list of supported format specifiers (e.g. jpeg) is in the documentation:
saveas(gcf,sprintf('%s - Title).png', NAME))
I worked it out! Thank you!
You are welcome. Please consider to substitue gcf by a figure handle returned from the figure() command. gcf can become very confusing when dealing multiple figures or if you have user interaction.

Sign in to comment.

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!