Run MATLAB code in HTML documents and insert output
docrun(html) runs MATLAB code blocks in the HTML document(s) html, and inserts the textual and graphical output.
Multiple documents may also be specified using docrun(html1,html2,...).
docrun(...,"Level",n) specifies the batching level n. With level 0 (default), all blocks in a document are run in a single batch. With level n, each level-n heading is run as a separate batch, with the workspace cleared and figures closed between batches. With level 7, each block is run as a separate batch.
docrun(...,"FigureSize",s) sets the default figure size s. Size is [width height] in default figure Units.
docrun(...,"Theme",t) sets the theme t. Available themes are none (as is, default), light, dark, and auto (responsive). For viewing as part of the MATLAB documentation, especially prior to R2025a, light mode works best. For viewing standalone, responsive mode works well.
html = docrun(...) returns the names of the HTML document(s) modified.
| Name | Description | Type | Required |
|---|---|---|---|
html➡️ |
HTML document(s), as an absolute or relative path; wildcards are supported | string(s) | yes |
n➡️ |
batching level, from 0 to 7; default is 0 | double | 🧪 |
s➡️ |
default figure size, in default figure Units |
double | |
t➡️ |
theme: none, light, dark, or auto; default is none |
string(s) | |
➡️html |
HTML document(s) modified, as an absolute path | string(s) |
docrun("mickey/pluto.html") processes a single HTML document mickey/pluto.html. Note that this path is relative.
docrun("C:\daisy\mickey\pluto.html") also processes a single HTML document, this time specified using an absolute path.
docrun("mickey/*.html") processes all HTML documents in mickey.
docrun("mickey/**/*.html") processes all HTML documents in mickey and its subfolders.
docrun(["mickey/pluto.html" "mickey/donald.html"]) processes multiple HTML documents.
docrun("mickey/pluto.html","Theme","light") generates output in light mode.
docrun("mickey/pluto.html","Theme","auto") generates responsive output -- light in light mode, dark in dark mode. If Theme is not specified -- or is specified as none -- then output is generated in the current MATLAB mode.
docrun("mickey/pluto.html","FigureSize",[600 400]) sets the default figure size to 600 wide by 400 high. The previous default figure size is restored when docrun completes.
Each run consists of 6 steps:
- Note pre-existing figures
- Read the HTML document
- Remove previous MATLAB output
- Process each MATLAB code block in turn
- Run the code
- Insert textual output
- Insert graphical output
- Write the modified HTML document
- Close non-pre-existing figures
You can include MATLAB code blocks in Markdown documents using ```matlab. The raw Markdown looks like:
```matlab
x = 0:0.1:10;
plot(x,sin(x))
```
These blocks are rendered with MATLAB syntax highlighting:
x = 0:0.1:10;
plot(x,sin(x)) docconvert transforms MATLAB code blocks in Markdown documents into <div>s with class highlight-source-matlab in HTML documents.
docrun runs arbitrary MATLAB code blocks in HTML documents. This is a potential vector for malicious attacks. Only run code from people or organizations you trust.
docrun evaluates each code block in turn in a MATLAB workspace that is private to the document. Variables created in earlier blocks are available for use in later blocks.
If an error occurs in one block then docrun rethrows the error as a warning (identifier docmaker:EvalError) and moves on to the next block. However, it is likely that an error in an earlier block will lead to further errors in later blocks.
If you wish to display but not evaluate MATLAB code, add trailing whitespace to the last line of the code block, and docrun will leave it be.
docrun captures messages to the command window, and monitors creation of new figures and changes to existing figures.
After each code block is executed, docrun inserts the messages and figure screenshots into the HTML document, immediately after the corresponding code block.
Messages are inserted as plain text, with markup removed.
m = magic(4)Figures are inserted, at the on-screen size.
f = figure();
peaksError messages are also inserted, with the offending statement indicated.
m = magick(4)docrun inserts MATLAB outputs into HTML documents in <div>s with class highlight-output-matlab. Previous outputs are removed.
Display scaling is a feature that adjusts the size of elements on screen to ensure they are easily readable and usable, regardless of the resolution of your display. This is particularly useful for high-resolution displays, where elements can appear too small to see comfortably at the native resolution.
docrun uses getframe to capture screenshots. getframe captures screenshots using native resolution, to maximize quality. In versions prior to 0.4, this led to inconsistencies in image sizes between applications and machines. From version 0.4, docrun includes scaled image sizes in the generated HTML.
home 🏠 | convert ➡️ | run 🏃 | index 📜 | delete ♻️ | workspace 👷 | about 🐣 | ©️ MathWorks 2024-2026