How to create reports containing text and figures with MATLAB
Asked Answered
B

4

9

I am using a MATLAB script to tune the control system on a machine. When the tuning is complete, I would like a report containing text (especially serial number, date/time and the values determined during tuning) and plots, especially transfer functions.

What do to you recommend?

Whatever solution I use should be compatible with the MATLAB compiler so I can distribute my solution to a team of field engineers.

Ideally the report will be a PDF document.

The MATLAB report generator does not seem to be the right product as it appears that I have to break up my script into little pieces and embed them in the report template. My script contains opportunities for the user to intervene and change values or reject the tune if plots don't look right and my hunch is that this will be difficult if the code runs from the report generator. Also, I fear code structure and maintainability will be lost if the code structure is determined by the requirements of the report template.

Please comment if my assumptions are wrong.

UPDATE

I have now switched to use the MATLAB Report Generator with release r2016b and it is working very well for my compiled code users. Unfortunately it means that colleagues who have a MATLAB licence need to buy the Report Generator too, to use my tools scripted.

Bunkmate answered 22/11, 2012 at 10:1 Comment(0)
M
4

As the MATLAB Report Generator's development manager, I am concerned that this question may leave the wrong impression about the Report Generator's capabilities.

For one thing, the Report Generator does not require you to break a script up into little pieces and run them inside a template. You can do this if you choose and in some circumstances, it makes sense, but it is not a requirement. In fact, many Report Generator applications use a MATLAB script or program to interact with a user, generate data in the MATLAB workspace, and as a final step, generate a report from the workspace data.

Moreover, as of the R2014b version, the MATLAB Report Generator comes with a document generation API, called the DOM API, that allows you to embed document generation statements in a MATLAB program. For example, you can programmatically create a document object, add and format text, paragraphs, tables, images, lists, and subdocuments, and output Microsoft Word, HTML, or PDF output, depending on the output type you select. You can even programmatically fill in the blanks in forms that you create, using Word or an HTML editor.

The API runs on Windows, Linux, and Mac platforms and generates Word and HTML output on all three, without the use of Word. On Windows, it uses Word under the hood to produce PDF output from the Word documents that it generates.

The latest release of the MATLAB Report Generator introduces a PowerPoint API with capabilities similar to the DOM API. If you need to include report generation in your MATLAB application, please don't rule out the MATLAB Report Generator based on past impressions. You may be surprised at just how powerful it has become.

Medeah answered 15/6, 2015 at 16:0 Comment(1)
I have now switched to the Report Generator and it works well.Bunkmate
A
3

I've done this quite a bit. You're right that MATLAB Report Generator is typically not a great solution. @Max suggests the right approach (automating Word through its COM interface), but I'd add a few extra comments and tips, based on my experiences.

  1. Remember that if you're going with this solution, you are depending that your end-users will be running Windows, and have a copy of Office on their machine. If you want to ultimately produce a PDF report, that will need to be Office 2010 or above.
  2. I would bet that you'll find it easier to automate the report generation in Excel rather than Word. Given that you're producing a report from MATLAB, you'll likely be wanting quite a lot of things in tables of numbers, which are easier to lay out in Excel.
  3. If you are going to do it in Word, the easiest way is to first (without MATLAB) create a template .doc/.docx file, which contains any generic text that will be the same for all reports and blank tables for any information. Turn on track changes, and insert empty comments at each point that you will be filling in information. Then within your report creation routine in MATLAB, connect to Word and iterate through each comment, replacing it with whatever data you wish.
  4. If you are learning to automate Excel from MATLAB, this page from the Excel Interop documentation is really helpful. There's an equivalent one for Word.
  5. Unlike @Max, I've never had good results by saving figures to an .emf file and then inserting them. In theory that does preserve editability, but I've never found that valuable. Instead, get the figure looking right (and the right size) in MATLAB, then copy it to the clipboard with print(figHandle, 'dbitmap') and paste to Excel with Worksheet.Range('A1').PasteSpecial.
  6. To save as a PDF, use Workbook.ExportAsFixedFormat('xlTypePDF', pathToOutputFile).

Hope that helps!

Argal answered 22/11, 2012 at 16:34 Comment(1)
the actual behaviour when inserting a figure from an .emf file varies with the version of Matlab and/or Word used. I used to get better results a couple of years ago than I do now. However, it does still work, so if it ain't broke...Mikol
S
2

I think you are right about the report generator.

In my opinion the fastest/easiest approach would be to generate the report in a html document. For that you just need the figures and write a text file, conversion should be trivial.

Quite similar approach would be to create a Latex file. And then create a pdf from it - though for this you'd need to install latex on your deployed machines.

Lastly you could use the good integration of Java in Matlab. There are several libraries you could use - like this. But I wonder if all the complication will be worth it.

Stagg answered 22/11, 2012 at 10:14 Comment(0)
M
1

Have you considered driving Microsoft Word through its ActiveX interface? I've done this in compiled Matlab programs and it works well. Look at the Matlab help for actxserver(): The object you want to create is of type Word.Application.

Edit to add: To get figures into the document, save them as .emf files using the -dmeta argument to print(), then add them to the document like this:

 WordServer.Selection.InlineShapes.AddPicture(fileName);
Mikol answered 22/11, 2012 at 10:12 Comment(3)
Thanks for the example. How do you discover all the services that word provides? Google isn't helping.Bunkmate
There is a fair amount of information on the Microsoft web site, but it's difficult to find. Try looking for 'Word Automation' or 'VBA Word'. Once you get a Word object you can call methods() on it in Matlab to see what it exposes, but that isn't everything.Mikol
The help available in recent versions of Word no longer documents this stuff, unfortunately. I believe the interface described in the O'Reilly book is pretty much unchanged (at shop.oreilly.com/product/9781565927254.do) EXCEPT that for Word 2010 the ActiveDocumentSaveAs() method has been replaced by ActiveDocumentSaveAs2(). You can also try recording a Macro (which facility is available, unaccountably, from the View ribbon tab!) and look at the generated VBA.Mikol

© 2022 - 2024 — McMap. All rights reserved.