function GLE_out = fig2gle(fname,FLAG_LEGEND,FLAG_BNW,FLAG_EDIT_GLE) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% FIG2GLE - convert a currently open MatLAB fig into a script %% for GLE, Graphics Layout Engine. Can optionally %% run command-line GLE to produce an EPS or PDF for insertion %% into beautiful, beautiful LaTeX %% %% GLE_out = fig2gle(fname, FLAG_LEGEND, FLAG_EDIT_GLE, FLAG_BNW) %% %% fname - name of the output file. Output will be: fname.pdf, %% fname.gle and fname.dat. Defaults to date + 'gle_output' %% %% FLAG_LEGEND - set to 1 to include a legend on the figure. %% Default is 1. %% FLAG_EDIT_GLE - set this flag to 1 to skip .dat file creation. %% use this to edit the GLE file without having to %% wait for the .dat file to finish writing. %% FLAG_BNW - set this to 1 to draw the figure in black and white %% will use different linestyles to represent %% red, green and blue. %% %% GLE_out - returns status of GLE output %% %% Standard Usage: %% %% 1) plot a figure in MatLAB or Octave %% 2) make it the active figure (in MatLAB, this means clicking on the figure; in Octave, this %% needs to be done with 'figure(x)') %% 3) command: fig2gle('May24_Intro_Comp_laser',1) %% 4) The local directory will have: %% May24_Intro_Comp_laser_1.dat - data file %% May24_Intro_Comp_laser_n.dat - up to n-data files %% May24_Intro_Comp_laser.gle - the GLE script %% May24_Intro_Comp_laser.pdf - PDF figure %% %% Change, modify, distribute as you see fit! %% Version 0.6 - written in 2008 by Cibby Pulikkaseril %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% v0.5 - initial script written Aug20/08 %% %% v0.6 - changed data write function to write separate files %% for each data set. This allows GLE to work with %% data sets of different sizes %% v0.65 - some minor changes with font sizes to look better in my thesis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% GLE Variables %% %% This contains the variables that determine sizes and styles for %% the GLE output. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% page_size = [17 13]; %% GLE page size in cm (note: need a certain min. size for linestyles!) graph_size = [page_size(1) page_size(2)]; %% in case you want to change the size of the graph on the page. graph_fullsize = 1; %% set to 1 to make graph full sized tit_hei = page_size(2)/10; %% title height tit_colour = 'black'; %% title colour tit_dist = page_size(2)/10; %% title distance from graph x_label_hei = page_size(2)/31; y_label_hei = page_size(2)/31; %% axis labels heights leg_hei = 0.4; %% legend height leg_position = 'tr'; %% legend offset leg_nobox = 'nobox'; %% set to 'nobox' for no border on the legend, or '' for the border xstep = page_size(1)/10; %% create step sizes for the document, so that the code ystep = page_size(2)/10; %% is independant of the page size. Not used. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Function arguments and FLAGS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if nargin < 4 FLAG_BNW = 0; %% this flag converts colour to black and white end if nargin < 3 FLAG_EDIT_GLE = 0; %% turn this flag on if you don't want to rewrite the data file end if nargin < 2 FLAG_LEGEND = 1; %% turn this flag on if you want a legend on your figure end if nargin < 1 fname = [datestr(date,'mmm') datestr(date,'dd') '_gle_output']; %% set the output name if not specified end output_format = 'pdf'; %% set to pdf or eps FLAG_AUTOCLEAN = 0; %% this flag = 1 deletes the data and GLE file, leaving only your EPS/PDF FLAG_OCTAVE = 1; %% set this to 1 if we're using OCTAVE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 %% Data Stripping section %% %% Tear all the pertinent settings, strings and data from the %% MatLAB figure, rending it to pieces with razor sharp aluminium claws %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 h = gca; %% return handle to current axis. x_lim = get(h,'XLim'); %% X-axis limits y_lim = get(h,'YLim'); %% Y-axis limits g = get(h,'XLabel'); %% X-axis label handle x_label = get(g,'String'); %% X-axis label g = get(h,'YLabel'); %% Y-axis label handley y_label = get(g,'String'); %% Y-axis label g = get(h,'Title'); %% Title label handley fig_title = get(g,'String'); %% Title label if FLAG_OCTAVE == 0 g = legend; %% start stripping the legend of its tags if size(g,1) ~= 0 %% if there IS a legend g2 = get(g,'Children'); L_string = get(g2(end),'String'); end end %% fLAG %%%%% Start stripping data %%%%%%%% g = get(h,'Children'); if FLAG_OCTAVE == 1 %% octave treats size differently n = length(g); else n = size(g,1); %% n is the number of datasets end FLAG_UNEVEN = 0; %% flag to check if datasets are the same length for ii = 1:n x_data{ii} = get(g(ii),'XData'); %% these are the x and y sets y_data{ii} = get(g(ii),'YData'); line_style{ii} = get(g(ii),'LineStyle'); %% linestyle line_width(ii) = get(g(ii),'LineWidth'); %% linewidth line_colour(ii,:) = get(g(ii),'Color'); %% line colour. notice awesome non-American spelling in variable. data_length(ii) = length(x_data{ii}); %% length of each dataset if data_length(1) ~= data_length(ii) %% if the length of any set is not the same as the first, set the flag. FLAG_UNEVEN == 1 end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 %% Parameter Translation %% %% change MatLAB's parameters into GLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% GLE provides text colours, unfortunately for ii = 1:n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if line_colour(ii,:) == [1 0 0] %% translate MatLAB RGB to GLE terms gle_line_colour{ii} = 'red'; elseif line_colour(ii,:) == [0 1 0] gle_line_colour{ii} = 'green'; elseif line_colour(ii,:) == [0 0 1] gle_line_colour{ii} = 'blue'; else gle_line_colour{ii} = 'black'; end %% line_colour %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if line_style{ii} == ':' %% translate line styles gle_line_style(ii) = 6; elseif strcmp(line_style{ii},'-') gle_line_style(ii) = 1; elseif strcmp(line_style{ii},'-.') gle_line_style(ii) = 8; elseif strcmp(line_style{ii},'--') gle_line_style(ii) = 3; else gle_line_style(ii) = 1; end %% line_style %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% gle_line_width(ii) = line_width(ii)/4; %% a little more faithful linewidth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if FLAG_BNW == 1 %% for publications that are in B&W, change all the colours to black gle_line_colour{ii} = 'black'; %% and adjust their style to be able to differentiate if line_colour(ii,:) == [1 0 0] %% translate MatLAB RGB to GLE terms gle_line_style(ii) = 3; elseif line_colour(ii,:) == [0 1 0] gle_line_style(ii) = 6; elseif line_colour(ii,:) == [0 0 1] gle_line_style(ii) = 1; else gle_line_style(ii) = 1; end end %% FLAG_BNW %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Write DATA %% %% This section creates a text file and writes the %% datasets as columns. %% The first set would be columns 1 & 2 %% The second set, 3 & 4, etc. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% first, we write data to data files %%%% these are named after the GLE filename root, %%%% as fname.dat if FLAG_EDIT_GLE == 0 %% can skip .dat file write if this is set to 1 for ii = 1:n fname_data{ii} = [fname '_' int2str(ii) '.dat']; %% create data filename if FLAG_OCTAVE == 1 M = [x_data{ii} y_data{ii}]; else M = [x_data{ii}' y_data{ii}']; %% attach all data sets as columns into one matrix. end dlmwrite(fname_data{ii},M,' '); %% write the data to the file end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 %% Write GLE File %% %% Create the GLE file. GLE supports a lot of options %% so adding more functionality here would be terrific. %% You have control over ticks, labels, legends. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% fname_gle = [fname '.gle']; %% output GLE fname fid = fopen(fname_gle,'wt'); s{1} = sprintf('!GLE file to plot MatLAB figure in %s\n',fname_gle); s{end+1} = sprintf('!Created on %s\n',date); s{end+1} = sprintf('\n'); %% aw, hell, why not? s{end+1} = sprintf('size %d %d\n',page_size(1),page_size(2)); s{end+1} = sprintf('\nbegin graph\n'); s{end+1} = sprintf('\tsize %d %d\n',graph_size(1),graph_size(2)); if graph_fullsize == 1 %% creates the graph at fullsize (well, 85%) s{end+1} = sprintf('\thscale 0.85\n'); s{end+1} = sprintf('\tvscale 0.85\n'); end s{end+1} = sprintf('\tnobox\n'); s{end+1} = sprintf('\txtitle "%s" hei %5.3f\n',x_label,x_label_hei); s{end+1} = sprintf('\tytitle "%s" hei %5.3f\n',y_label,y_label_hei); %% init the string variable for ii = 1:n s_data_terms = sprintf('d%d=c1,c2 ',ii); %% create a string to load the data sets into d1, d2, etc.. s{end+1} = sprintf('\tdata %s %s\n',fname_data{ii},s_data_terms); end %s{end+1} = sprintf('\ttitle "%s" hei %d color %s dist %d\n',fig_title,tit_hei,tit_colour,tit_dist); %% Forget it - titles are useless for me, anyway. s{end+1} = sprintf('\txaxis min %5.3f max %5.3f\n',x_lim(1),x_lim(2)); s{end+1} = sprintf('\tyaxis min %5.3f max %5.3f\n',y_lim(1),y_lim(2)); s_line = ''; %% this loop is to generate commands that plot the data sets %%%%%%%%%%%%%%%% Create line with linestyles and colours for ii = 1:n s_line = [s_line sprintf('\td%d line lstyle %d color %s msize %5.3f\n',ii,gle_line_style(ii),gle_line_colour{ii},gle_line_width(ii))]; end s{end+1} = sprintf(s_line); %%%%%%%%%%%%%%%% If we need a legend, do that, too %%%%%%%%%%% if FLAG_LEGEND == 1 s{end+1} = sprintf('\tkey pos %s %s hei %5.3f\n',leg_position,leg_nobox,leg_hei); s_legend = ''; for ii = 1:n %% write the lines, with dummy text that must be replaced by hand s_legend = [s_legend sprintf('\td%d key "Line %d"\n',ii,ii)]; end s{end+1} = sprintf(s_legend); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% s{end+1} = sprintf('end graph\n'); for jj = 1:length(s) fprintf(fid,s{jj}); end fclose(fid); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Execute GLE to make final output %% %% Runs GLE with MatLAB's command operator. %% Option to delete GLE and data file, if that's how %% you like to roll. Keeping GLE files around is a better move, %% as you can make edits to them quite easily. Who am I %% to judge, though? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if FLAG_OCTAVE == 1 gs = sprintf('gle -d %s %s',output_format, fname_gle); %% run GLE system(gs); if FLAG_AUTOCLEAN == 1 %% if FLAG_AUTOCLEAN == 1, delete GLE and dat file gs = sprintf('!del %s\n!del %s\n',fname_gle,fname_data); eval(gs); end if size(ls([fname '.pdf']),1) == 0 GLE_out = 'Your conversion has been futile'; else GLE_out = 'You are a succesful little monkey!'; end else gs = sprintf('!gle -d %s %s',output_format, fname_gle); %% run GLE eval(gs); if FLAG_AUTOCLEAN == 1 %% if FLAG_AUTOCLEAN == 1, delete GLE and dat file gs = sprintf('!del %s\n!del %s\n',fname_gle,fname_data); eval(gs); end if size(dir([fname '.pdf']),1) == 0 GLE_out = 'Your conversion has been futile'; else GLE_out = 'You are a succesful little monkey!'; end end %if FLAG_OCTAVE