views:

99

answers:

4

I like to create a "report generation" script in Matlab.

Suppose we have a Matlab array, data and we want to export the following to a .tex file: "The information in the first element of data is X." This would be followed by a plot of X.

I have already tried help latex in Matlab and aware of the various packages on Matlab file exchange. However I have seen nothing so far that will allow me to export both text and plots in the same Matlab script to a .tex file.

A: 

Exporting figures from Matlab to a .tex file is just a matter of exporting the figure to an appropriate format and then including the figure file in the .tex file. Would something like the code listed below work for your needs?

Using LaTeX to generate dvi:

% Include code to write text to .tex file (fid is assumed to be the file id of the .tex file)
print(gcf,'figure1','-depsc','-r100');
fprintf(fid,'\includegraphics[width=4in]{figure1.eps}\n');

Using pdfTeX to generate pdf:

% Include code to write text to .tex file (fid is assumed to be the file id of the .tex file)
print(gcf,'figure1','-djpg','-r100');
fprintf(fid,'\\includegraphics[width=4in]{figure1.jpg}\n');
arun
@arun, no this would not work because it only deals with figures, not text and figures.
Elpezmuerto
@Elpezmuerto: Perhaps this is a very specific fix, but would work for your example. fprintf(fid,['The information in the first element is ' num2str(X(1)) '\n']);This is very tedious to generate reports/documentation. Could you elaborate on your broader needs?
arun
@arun, We have common metrics across reports and instead of rewriting the report or copy and editing the report (both tedious), I want to create a Matlab script that generate a starting point for the results section of the report. It would state the metrics and display the appropriate figure.I don't want to use matlab2tikz or something similar everything we do a new analysis. I like to run this script which generates all the plots and basic text. I understand this can be pretty ambitious.
Elpezmuerto
+1  A: 

Are you aware of matlab2tikz? i've used it extensively for my PhD-Thesis, albeit only for exporting single plots. But I guess it should be easily possible to whip something up that combines the power of MATLABs LaTeX export capabilities.

Habi
I have used it before, but just as you stated with exporting single plots.
Elpezmuerto
+3  A: 

The publish function may work for you.

Create this script, foo.m:

%%
% The information in the first element of data is X.

plot(X)

And publish it to LaTeX:

>> publish foo latex
Matthew Simoneau
@Matthew, publish seems promisingly however by using comments it is not very dynamic. I want to export what "X" is. Suppose X = 50, then the LaTeX output would be:"The information in the first element is 50"
Elpezmuerto
`publish` can do that easily enough, just put X(1) on a line of its own (not in a comment), it'll be included in the output listing. I think `publish` is the usual way of doing this in the MATLAB world, it's worth going and looking at the documentation.
rescdsk
+1  A: 

You might want to take a look at this article published in TUGboat (the official magazine of the TeX Users Group):

http://www.tug.org/TUGboat/Articles/tb24-2/tb77seta.pdf

Generating LaTeX documents through Matlab (S. E. Talole and S. B. Phadke)

Good luck!

yCalleecharan