Tuesday, July 8, 2008

Convert a series of Matlab figure files in to jpg

This program converts a set of fig files located in a directory in to jpg files. The program runs through the directory and gets a list of fig file names. These files are then read using the ‘openfig’ command and the output is written using ‘saveas’ command. For example, a file named ‘image.fig’ will be read and saved as ‘image.fig.jpg’. The only variable that needs to be changed is the figdirectory

Bonus: You can use the parsing directory structure to read any kind of files located in a directory.



%%Matlab program - fig2jpg
figdirectory = 'C:\Documents and Settings\username\Desktop'

fullpath = sprintf('%s/*.fig',figdirectory)
d = dir(fullpath);
length_d = length(d)
if(length_d == 0)
disp('couldnt read the directory details\n');
disp('check if your files are in correct directory\n');
end

startfig = 1
endfig = length_d

for i = startfig:endfig
fname = d(i).name;
fname_input = sprintf('%s/%s',figdirectory,fname)
fname_output = sprintf('%s/%s.jpg',figdirectory,fname)
saveas(openfig(fname_input),fname_output,'jpg');
end