Math & ScienceLoading MatLAB .fig files in Octave, then exporting to LaTeX

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
cibbuano
Posts: 4
Joined: Thu Mar 12, 2009 12:36 am

Loading MatLAB .fig files in Octave, then exporting to LaTeX

Post by cibbuano »

If you're like me, you've been stuck in the dusty halls of the university system for years, the cynicism etched on your face, a perpetual snarl tugging at the corner of your mouth.

At Uni, we have a site-wide license for MatLAB, which is fine. MatLAB is a good platform for running simulations, even if it's getting on my back. Why is it taking 140 MB of RAM when there's nothing running?

At home, I do what many students do: install Octave because it's free and it's fast. Yes! - running on Ubuntu, octave runs my simulation like lightning, despite the feeble processing power of my $500 laptop.

The problem is - or, at least, the folly on my part is - that I've saved many of my results as MatLAB figures, ready to be exported into papers or reports. Yes, now it seems ridiculous, but at one point I thought I was being dastardly clever.

I'm now writing my thesis, and I've come upon a solution for extracting data from MatLAB .fig files, exporting to a LaTeX-friendly PDF.

I use Graphics Layout Engine (GLE), which produces beautiful PDFs from simple script files.

With this installed, I've written two scripts that help me export my MatLAB results into lovely PDFs.

OCTread_FIG is a function that takes a .fig file, the name stored in a string called fname, then strips out the data and plots it.

Once Octave has plotted the figure, I use fig2gle to convert the figure into a GLE script and data files, then run GLE to produce a PDF.

This may not seem like such an easy solution, but it has advantages:

- the data is stored in ASCII .dat files, which you can manipulate very easily.
- if the graphs need to be altered (labels, legends), it's easily done by running GLE again (eg. gle -d pdf filename.gle)
- it allows me to use Octave, instead of shelling out 100 clams for the irritating student version of MatLAB.

I hope this is of use to someone... ideally, even easier solutions will be developed!

OCTread_FIG page
fig2gle page

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

drgz
Posts: 44
Joined: Sat Apr 18, 2009 8:40 pm

Re: Loading MatLAB .fig files in Octave, then exporting to LaTeX

Post by drgz »

Thanks for fig2gle code, I assume it will turn out handy for my thesis as well, as I consider the exported plots from MATLAB to be ugly and not good enough for a thesis :)
pgadamczyk
Posts: 2
Joined: Fri May 10, 2013 10:53 pm

Loading MatLAB .fig files in Octave, then exporting to LaTeX

Post by pgadamczyk »

For any recent users, here is a reconstructed version of the original OCTread_Fig.m function/script from the original author. Thanks, Cibby!

Code: Select all

function [D,textItems] = OCTread_FIG(fname,FLAG_PLOT)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%   [D,items] = OCTread_FIG(fname,FLAG_PLOT)
%%
%%   OCTread_FIG is a simple function that extracts the plot data from
%%   a MATLAB .fig file. It does this by loading the file, which is
%%   in struct-format, then parsing down the structure to pull out the data
%%  
%%   D = a struct array containing the plot data, eg.
%%       plot(D(1).x,D(1).y) would plot the first set of data
%%
%%   textItems = a struct array of strings containing text in the figure.
%%               the axis labels come into here, and I just store them all.
%%
%%   fname = the name of the *.fig file (used for MATLAB R2011a
%%   FLAG_PLOT = set to 1 to plot the data (default is 0)
%%
%%
%%   Working in Octave 3.6.2, but when a figure contains a legend, Octave will
%%   pop-up a bunch of warnings as it tries to load MATLAB files.
%%
%%   modify as you like! 
%%   Cibby Pulikkaseril, 2013
%%   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ~exist('FLAG_PLOT')
	FLAG_PLOT = 0;
end

aa = load(fname);							%% load file into memory, and start climbing down the struct
aName = fieldnames(aa);
bb = getfield(aa,aName{1});

cc = bb.children;
dd = cc.children;

nItems = length(dd);
kk = 1;								%% counter for plots
jj = 1;								%% counter for text
textItems = {};

if FLAG_PLOT == 1
	figure(1);
	hold on;
	col = repmat('rgmcym',1,round(nItems/6)+1);
end

for ii = 1:nItems

	ee = dd(ii);
	
	if strcmp(ee.type,'graph2d.lineseries')				%% check if 2D data
		ff = ee.properties;
	
		D(kk).x = ff.XData;
		D(kk).y = ff.YData;
				
		if FLAG_PLOT == 1
			plot(D(kk).x,D(kk).y,col(kk));
		end
		kk = kk + 1;
	elseif strcmp(ee.type,'text')	
		ff = ee.properties;
		if isfield(ff,'String')
			textItems{jj} = ff.String;
			jj = jj + 1;
		end
	end
	
end


pgadamczyk
Posts: 2
Joined: Fri May 10, 2013 10:53 pm

Loading MatLAB .fig files in Octave, then exporting to LaTeX

Post by pgadamczyk »

Also, here is a link to an expanded version I built based on Cibby's code above. This one includes features such as subplots, markers and marker sizes, scatter plots, text, patches, surfaces, and legends:
http://stackoverflow.com/a/16490536/2371472

Good luck!
-Peter
bayoislands
Posts: 1
Joined: Wed Jun 26, 2013 6:46 am

Re: Loading MatLAB .fig files in Octave, then exporting to L

Post by bayoislands »

Hi pgadamczyk, thanks for providing the code above; I can't find your expanded version on the stackoverflow link you provide. Is it on another link somewhere? Cheers, Paul.
snapey1979
Posts: 1
Joined: Thu May 12, 2016 5:36 pm

Loading MatLAB .fig files in Octave, then exporting to LaTeX

Post by snapey1979 »

For some reason, the post on Stack Overflow was deleted by a moderator. I found the code useful, so I've stuck it on a Github repo https://github.com/rsnape/OpenMatlabFigureInOctave

Obviously the original authors are acknowledged - I'll leave it up there unless anyone objects.
Post Reply