Write a figure to a file automatically in MATLAB
Asked Answered
H

4

11

Does anyone know if it's possible to automatically write a figure out to a .eps file in MATLAB?

I'm running a script that produces a large number of graphs, and it'd be nice if I didn't have to manually save each one!

Harpy answered 3/3, 2009 at 15:13 Comment(1)
Perhaps related here.Haim
G
10

print function does that:

Print figure or save to specific file format...

print(filename,formattype) saves the current figure to a file using the specified file format, such as print('BarPlot','-dpng'). If the file name does not include an extension, then print appends the appropriate one.

print(filename,formattype,formatoptions) specifies additional options that are available for some formats.

print prints the current figure to the default printer...

Gullett answered 3/3, 2009 at 15:15 Comment(1)
Thanks, saves me a lot of effort =]Harpy
C
10

print or saveas will do the trick.

saveas(fig_handle, 'filename','eps')
print('-deps',fig_handle)
print -deps 1

If you want to specify the output file name, you're better off using saveas.

Certain answered 4/3, 2009 at 14:10 Comment(1)
actually, you can specify the filename just fine with -print -epsc filenameHarpy
C
3

This was answered in this other question, using the PRINT command. Although that question dealt with making .tiff images, it should be straightforward to modify the code given in those answers to write a .eps.

Charioteer answered 3/3, 2009 at 15:14 Comment(2)
I'm not loading an image in or out, this is a set of automatically generated graphs, so I'm not sure the same syntax applies.Harpy
Although the text of the question just shows IMREAD and IMWRITE, some of the answers show how to use the PRINT function to output a plotted image.Charioteer
O
0

Suppose, you are generating N numbers of figures in a loop, then you should try the command line:

saveas(gca,sprintf('Figure%02d.pdf',N )); it produces N figures Figure1.pdf - FigureN.pdf saveas(gca,sprintf('Figure%02d.eps',N )); it produces N figures Figure1.eps - FigureN.eps

in place of gca one can use gcf also. First command line is a better solution.

Hope this will solve your issue.

Oedipus answered 6/3, 2014 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.