1

I have a list of .fig files in a directory.

How do I write a simple matlab function that converts all the .fig files to .jpg files automatically?

Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17
  • 1
    Read the manual! There is a function to open figures and there is a function to save figures into images. – Daniel Feb 23 '16 at 10:38
  • I would strongly suggest saving matlab figure to `.png` instead of `.jpg`. The `jpg` compression result in a lot of aliasing on your figure, which you wouldn't get with `.png` files. – Hoki Feb 23 '16 at 13:24

2 Answers2

6

Matlab figs are just matrices which you have to load into Matlab to interpret and convert, so you can try something like this:

fig=openfig(FileName,'new','invisible');
saveas(fig,OutputFileName.jpg,'jpg')
close(fig);

The 'invisible' option does not open the fig in a plot, so it saves memory and time.

GameOfThrows
  • 4,510
  • 2
  • 27
  • 44
2

GameOfThrows' answer was useful in saving a single .fig file to .jpg

To loop through all the .fig files, this worked for me:

//obtain the files with .fig extension
files = dir('*.fig');

//loop through the .fig files
for i=1:length(files)

   //obtain the filename of the .fig file
   filename = files(i).name;

   //open the figure without plotting it
   fig = openfig(filename, 'new', 'invisible');

   //save the figure as a jpg
   saveas(fig, 'example.jpg');

   //close the figure so that the next could be opened without some java problem
   close;

end
Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17