Table of Contents
What Live Scripts Are
Live scripts are MATLAB files that combine executable code, formatted text, equations, images, and outputs in a single document. They have the file extension .mlx and are edited in the Live Editor, not in the classic text-based Editor. You can recognize them because they display code and rich text in separate sections, and you see results directly under the code.
Live scripts are especially useful when you want to explain what your code is doing, document your work as you go, or create instructional or report-style documents without leaving MATLAB. Unlike ordinary scripts, live scripts can contain headings, formatted paragraphs, and embedded graphics in the same file where you run your calculations.
Creating a New Live Script
You typically create a live script from the MATLAB Desktop. You can start from the Home tab by selecting the option to create a new live script. MATLAB then opens a new .mlx document in the Live Editor, ready for you to type code and text.
You can also convert an existing script (.m file) into a live script. One way is to open the .m file in the Editor and use the provided option to convert it to a .mlx file. MATLAB keeps your original code and creates a new live script version. In the converted document, your existing comments and code layout appear in code sections, and you can then enrich them with formatted text.
When you save a live script, MATLAB stores it as a .mlx file. You can run it like any other script, either section by section or all at once. The results stay visible in the document itself.
Difference Between Live Scripts and Ordinary Scripts
A live script and an ordinary script can contain essentially the same MATLAB code. The difference is how they are stored, edited, and presented. An ordinary script is a plain text file with extension .m, which you edit in the standard Editor. A live script is a binary formatted document with extension .mlx that you edit in the Live Editor.
In a live script, outputs, figures, and formatted narratives are shown inline, underneath or beside the corresponding code. In a plain script, all formatting is done only with comments, and outputs appear in the Command Window and in separate Figure windows.
From the point of view of execution, MATLAB interprets the code inside live scripts in the same way as it interprets code inside ordinary scripts. Any valid script code that runs in a .m file can also run in a live script, as long as you keep it in code regions. The additional rich text, headings, and other elements are ignored by the MATLAB interpreter and serve only for presentation and explanation.
Code and Text Sections in Live Scripts
Live scripts are organized into blocks of code and blocks of text. Code blocks are where you write MATLAB commands and expressions. Text blocks are where you write explanations, headings, and other narrative material.
You can add a text block above or below any code block. The Live Editor shows a different visual style for text and code, so it is clear where each kind of content begins and ends. To change a block from code to text or from text to code, you use the Live Editor controls rather than typing special markers.
Live scripts also support sections inside code, which you can run individually. Sections are marked similarly to standard script sections, but in the Live Editor you typically work with them through the interface rather than typing separators manually. Each section can have its own explanatory text immediately above or below, which makes it convenient to describe the purpose of each step of your analysis.
Writing and Formatting Rich Text
The main advantage of live scripts is their rich text capabilities. Inside text blocks you can write multi paragraph explanations, introductions, or conclusions, and you can format the text with different styles. For instance, you can make text bold or italic, or you can change font and color. You can also insert headings of different levels to organize your document into logical parts.
Unlike comments in plain scripts, text in live scripts does not need to start with a % character. You simply switch to a text block and start typing. MATLAB treats this text as documentation only. The code interpreter does not execute anything in a text block, so you are free to write natural language.
You can also adjust alignment, create numbered or unnumbered lists, and insert horizontal spacing, depending on the formatting tools available in the version of MATLAB you use. All of this formatting is saved with the .mlx file. When you run the live script later, the layout and text remain exactly as you set them, together with updated outputs.
Embedding Equations and Mathematical Notation
Live scripts support typeset mathematical expressions using LaTeX like notation. Inside text regions you can insert inline equations that appear within a sentence, or display equations that appear centered on their own line.
For example, you might want to document the formula for a quadratic function. In a live script text block you can insert a formatted equation that appears as something like
$$
y = ax^2 + bx + c
$$
with proper mathematical layout, rather than using plain ASCII text such as y = ax^2 + bx + c.
The Live Editor typically provides an equation button or menu that lets you insert an equation object. Inside that object you use LaTeX style commands to build fractions, superscripts, subscripts, Greek letters, and other mathematical symbols. These equations are not executable code, they exist purely for documentation and display.
You can mix equations with normal text. For instance, you might write a description that includes an inline expression such as $e^{i\pi} + 1 = 0$ to illustrate a concept, while the actual code that computes related values remains in separate code blocks.
Displaying Results Inline
When you run code in a live script, results appear directly below the code block by default. This inline display is one of the central features of live scripts. Numerical outputs, tables, and text outputs from functions appear embedded in the document. This makes it easy to see which code produced which result.
For example, you might write code like
x = 0:0.1:2*pi;
y = sin(x);
max_y = max(y)
When you run this block in a live script, MATLAB shows the value of max_y right under the code. If you plot data, the figure can also appear inline, within the same document, unless you configure it to open in a separate window. The combination of code, output, and explanation makes the live script resemble a scientific notebook.
You can usually clear or hide individual outputs if the document becomes cluttered, and you can re run sections to refresh outputs after changes to the code. Since the outputs are stored with the .mlx file, you can save a live script with all its current results and share it with others, who can see the outputs even before running the code themselves.
Including Images and Other Media
Live scripts allow you to insert images into text sections. This may be useful to include diagrams, screenshots, or any other visual materials that help explain your analysis or algorithm. You can place images above or below code blocks, or between paragraphs of text.
When you insert an image, MATLAB stores it as part of the live script file. The image is not executed, of course, but appears in the final document when it is opened. You can accompany images with captions or explanatory text, similar to a report or article.
In addition to images, some MATLAB versions support embedding other rich content such as hyperlinks. You can insert a link to an external website or to another MATLAB file or documentation page. This makes it possible to connect your live script to external references or related files in your project.
Using Live Scripts for Teaching and Reports
Because live scripts combine narrative, code, and results, they are well suited for educational materials and technical reports. In a teaching context, an instructor can prepare a live script that walks through a topic step by step. Each section may begin with a description, followed by example code and the resulting output or plots, all in one place.
For your own work, you can use live scripts as lab notebooks. You can describe your goals, record each experiment you run in code, and keep the outputs tied to the corresponding explanations. This makes it easier to remember what you did and why you did it, especially when you come back to the work after some time.
Live scripts can also help you prepare reports. Once you have code and results in place, you can refine the surrounding text to create a readable document. In many cases you can export a live script to formats that are suitable for sharing with people who do not use MATLAB, such as PDF or HTML, while preserving the appearance of code, figures, and formatted text.
When to Prefer Live Scripts
Live scripts are ideal when you want a single file that both runs and explains your work. You would usually choose a live script over a plain script if you are exploring data interactively, writing tutorials for others, or documenting a workflow in a way that needs to be read and understood.
However, for simple utility code or scripts that are only used internally by other programs and do not need extended documentation, an ordinary script is often sufficient. In practice, you may use both types together in one project. A live script can call functions and scripts in .m files while presenting the overall narrative and results.
Knowing when to use live scripts allows you to take advantage of the Live Editor features without replacing the normal coding patterns you already use.
Important points to remember:
Live scripts use the .mlx extension and open in the Live Editor with rich text, equations, and inline outputs.
Code executes only in code blocks. Text blocks, equations, and images are for documentation and do not run.
Live scripts are best for explanations, teaching materials, and report style documents that mix narrative with executable MATLAB code.