John
John

Reputation: 5915

Can one dynamically add sections to a matlab publish script?

I have a matlab script, where I would like to dynamically create sections in my matlab publish.

At present, the only way I know to create a section break, is to put code like this in my script:

%% This is a section break

I'd like to run publish on my script, and have the section breaks get added as part of the publish. For instance. Say I had the following script:

breaks(1).name = 'This is section break 1.';
breaks(2).name = 'This is section break 2.';

for ix = 1 : numel(breaks)
   functionThatInsertsSectionBreakTitle(breaks(ix).name);
   fprintf('Some random processing associated with break %d.\n', ix);
end

I would like to call publish on that script, and end up with a document that looks something like:

This is section break 1.

Some random processing associated with break 1.

This is section break 2.

Some random processing associated with break 2.

Obviously I could do this by writing a script that writes a script that then gets executed by publish. I was hoping for something a bit more direct. Am aware of the report generation toolbox, which I would hope would cleanly handle this type of scenario. Alternatively, if the new (as of R2016a) Live Script handles this use case, that's a fine answer as well.

Upvotes: 0

Views: 512

Answers (1)

NicoBernard
NicoBernard

Reputation: 362

One way to address this problem is by displaying html code in the command output (documented here).

In your example, the code would look like this:

breaks(1).name = 'This is section break 1.';
breaks(2).name = 'This is section break 2.';

for ix = 1 : numel(breaks)
    disp(['<html><h2>' breaks(ix).name '</h2></html>']);
    fprintf('Some random processing associated with break %d.\n', ix);
end

This is incredibly useful when you want to get results to be displayed with a custom layout, such as a table. And it avoids the need of having a Matlab Report Generator license...

Upvotes: 1

Related Questions