Math & Science ⇒ Loading MatLAB .fig files in Octave, then exporting to LaTeX
Loading MatLAB .fig files in Octave, then exporting to LaTeX
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
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
Re: Loading MatLAB .fig files in Octave, then exporting to LaTeX

-
- Posts: 2
- Joined: Fri May 10, 2013 10:53 pm
Loading MatLAB .fig files in Octave, then exporting to LaTeX
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
-
- Posts: 2
- Joined: Fri May 10, 2013 10:53 pm
Loading MatLAB .fig files in Octave, then exporting to LaTeX
http://stackoverflow.com/a/16490536/2371472
Good luck!
-Peter
-
- Posts: 1
- Joined: Wed Jun 26, 2013 6:46 am
Re: Loading MatLAB .fig files in Octave, then exporting to L
-
- Posts: 1
- Joined: Thu May 12, 2016 5:36 pm
Loading MatLAB .fig files in Octave, then exporting to LaTeX
Obviously the original authors are acknowledged - I'll leave it up there unless anyone objects.