MATLAB Answers

Programmatically run and export live script

176 views (last 30 days)
I have a set of live scripts that I can save as html and publish for a client to inspect. Right now, I do that by opening one script at a time, hitting F5, and then clicking Save As >> html to save it. I'd like to automate that process. I was hoping publish('script.mlx','html') would work, but it doesn't seem to. I can save the *.mlx as .m and publish that, but it changes the layout and formatting, and I'd like to keep those.

  0 Comments

Sign in to comment.

Accepted Answer

Baptiste Ottino
Baptiste Ottino on 7 Aug 2017
Hi, I'm late to the party, but I spent hours and hours on this issue, and by pure luck I just found the answer. So in hope someone reads this, here is the function you are looking for. It certainly is the one called by the "Save As" button.
doc matlab.internal.richeditor.openAndConvert
Best regards.

  8 Comments

Show 5 older comments
Timothy Koehler
Timothy Koehler on 27 Sep 2019
That option does work for saving the live script. The LiveScripts save their state, though, so in order to programmatically update them, I would think to use the following two commands:
matlab.internal.liveeditor.executeAndSave(filename);
matlab.internal.liveeditor.openAndConvert(filename,'report.pdf');
The second command works fine, but the first command yields:
Error using matlab.internal.liveeditor.LiveEditorUtilities.execute (line 12)
Java exception occurred:
java.io.IOException: java.lang.RuntimeException: LIveCodeFileReader cannot find package to open
at com.mathworks.services.mlx.MlxFileUtils.getCode(MlxFileUtils.java:183)
Any advice, or is there an alternate way to execute the live script?
Jianyong Wen
Jianyong Wen on 8 Nov 2019
try this way:
import matlab.internal.liveeditor.LiveEditorUtilities
[javaRichDocument, cleanupObj, executionTime] = LiveEditorUtilities.openAndExecute(which(filename));
LiveEditorUtilities.saveas(javaRichDocument, 'report.pdf');
Timothy Koehler
Timothy Koehler on 8 Nov 2019
That option resulted in a timeout when trying to run the saveas() function.
However; you gave me a path to the right solution! If I simply use:
matlab.internal.liveeditor.executeAndSave(which(filename));
instead of
matlab.internal.liveeditor.executeAndSave(filename);
, then the executeAndSave command will work. Apparently, it needs the full path to the .mlx file. Once that command completes, the openAndConvert() function works properly to generate the report.
Thanks for putting me on the right path.

Sign in to comment.

More Answers (0)

Sign in to answer this question.