Is there a way to dynamically save a plot figure into a jpeg file without having to type the filename i would be saving it into?

I am executing a code that generates a plot figure, which I save using the following command: print '-djpeg' '-f2' '-r300' _f2
Where _f2 is literally the name of the jpeg I will be saving my plot into.
I wonder if there's a way of doing this dynamically, what i mean is that instead of entering the file name literally I could pass a string containing the file name, so I can execute this could without having to type each time the file name in my code.
As a previous step to generating the plot figure, I load a file and analyze it. Each file is identified with a frequency (filename=06000). My idea is to copy the filename (06000) into a string, so i can identify each jpeg with its corresponding file (06000.jpeg)
Thanks!!

 Accepted Answer

saveas(figureHandle,['filename' num2str(numberId) '.jpg']);
For more info:
help saveas
Cheers!

4 Comments

Hey José-Luis
I tried that command but my problem is that since im analyizing data from different hours from different days, id like to identify the jpeg file with that info. I'll set you an example for a file name (F11.5Mhz_h03) where these 11.5 and 3 i get them from filename and path of the file i am currently analyzing.. So is there a way that i could use this?
saveas(figureHandle,['F%s_h%s' num2str(freq)num2str(time) '.jpg']);
Thanks !!
Would this work:
saveas(figureHandle,['filename' num2str(oneValue) '_' num2str(anotherValue) '.jpg']);
You can concatenate as many strings as you'd like inside the [] operator. If you want to extract the numbers from the filename, you can look at
help regexp
Cheers!
Ok the problem I had was that I wasn't using the figureHandle correctly I was using 'f3' instead of 'figure(3)'.
Now it's working perfectly and you have saved me a huge amount of time!!!
Thank you!!
Strange, you wouldn't want to do that. You must have a figure handle already before you pass it into saveas(). Like this
f3 = figure(3);
% Now plot stuff.
% Now save figure.
saveas(f3,....);
If you did this
saveas(figure(3), ......);
it would probably create a brand new blank figure when it hits the figure(3) code, and then you'd be saving that (a blank figure).

Sign in to comment.

More Answers (2)

Use sprintf() to construct your filename and then use export_fig() to save it:
baseFileName = sprintf('F%s_h%s.jpg', numberId, numberId);
fullFileName = fullfile(yourFolder, baseFileName);
export_fig(figureHandle, fullFileName); % Best way.
saveas(figureHandle, fullFileName); % Alternative way

2 Comments

Hey, I tried the code as you said, but i dont know if im doing it right!
baseFileName = sprintf('F%s_h%s.jpg', freq, time);
fullFileName = fullfile('C:\To', baseFileName);
It loads the graph but doesnt save into a file.This is the message i get from executing the code:
Undefined function 'export_fig' for input arguments of type 'char'.
Correct me if im wrong, but here's what i understand for:
  • yourfolder i put the path to my current folder
  • figureHandle i put the figure id.
Thanks!
Undefined function 'export_fig' for input arguments of type 'char'.
It look like you just ran the code and didn't read the FAQ. In the FAQ it explains how you can download export_fig. Anyway, here is the direct link: http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig Or you can go to the front page of the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/ because it's always the most downloaded submission every month.

Sign in to comment.

Hi,
with e.g.: freq=1 time=103 Filename = ['F' num2str(freq) '_h' num2str(time) '.jpg'] =>Filename =F1_h103.jpg then saveas(gcf, Filename)
this should work
regards,J

5 Comments

Hello Jürgen,
What if the variables 'freq' and 'time' are strings?
Thanks!
Hi then it is easier, because you can use the string directly :
freq='1', time='103', Filename = ['F' freq '_h' time '.jpg'] then use the saveas
That was from 9 years ago. Now you can use the new exportgraphics() function.

Sign in to comment.

Asked:

on 18 Aug 2012

Commented:

on 24 Mar 2021

Community Treasure Hunt

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

Start Hunting!