Live Scripts & Apps
Plain .m scripts are perfect for code, but sometimes you want a document (code, results, plots and explanations together) or a tool that people without programming experience can click through. MATLAB offers the Live Editor for the first and App Designer for the second. Both are MATLAB desktop features; this lesson explains how they work, what the code behind them looks like, and what you can use in Octave instead.
| Feature | File type | Purpose | In Octave |
|---|---|---|---|
| Live script | .mlx (or plain-text .m, R2025a+) |
Notebook-style document: formatted text, code, inline output | No Live Editor; use %% sections + publish, or Jupyter |
| Live function | .mlx |
A function with formatted help | No |
| Live controls / tasks | inside a live script | Sliders, drop-downs, point-and-click tasks that generate code | No |
| App Designer app | .mlapp |
Drag-and-drop GUI, saved as a class | No App Designer |
| Programmatic app | .m |
uifigure + uibutton ... built in code |
Partly: figure + uicontrol GUIs work |
Live scripts
A live script mixes text blocks (headings, formatted paragraphs, equations, hyperlinks, images) with code blocks. When you run a section, its output (printed values, tables, figures) appears right next to the code or inline below it, so the file reads like a lab report. Typical uses are teaching material, exploratory analysis and sharing results with colleagues who want to see both the method and the answer.
Things worth knowing:
- Sections. A live script is divided into sections; you can run one section at a time (Ctrl+Enter) and re-run it after changing a value, without restarting the whole script.
- Controls. Insert a slider, spinner, check box, drop-down or edit field in place of a literal value. Changing the control re-runs the section, which turns a script into a small interactive explorer.
- Live tasks. Point-and-click blocks (Clean Missing Data, Find Local Extrema, Create Plot, ...) that generate ordinary MATLAB code you can inspect and keep.
- Export. Save as HTML, PDF, Word, LaTeX, Markdown or a Jupyter notebook (File > Export), or call
exportfrom code. - File format. Classic live scripts are
.mlxfiles: zipped XML, not readable as text and awkward in version control. Since R2025a MATLAB can also save live scripts in a plain-text.mformat, which diffs and merges like any other source file. - Local functions can be defined at the end of a live script, just like in a plain script.
The portable alternative: sections and publish
You get much of the same benefit with a plain script. A line that starts with %% begins a section (a "code cell"), which the MATLAB editor (and Octave's) can run on its own. The text after %% becomes a heading, and the comment lines below it become paragraphs when you publish the script to HTML. publish exists in both MATLAB and Octave:
%% Cooling of a cup of tea
% Newton's law of cooling: the temperature approaches the room
% temperature exponentially, with time constant *tau*.
%% Parameters
room = 21; start = 90; tau = 12; % minutes
%% Simulation
t = 0:5:30;
T = room + (start - room) * exp(-t / tau);
disp([t; T]')
plot(t, T, '-o'), xlabel('t (min)'), ylabel('T (C)')publish('cooling.m', 'html') % creates html/cooling.html with code, output and figuresThe runnable part of that script:
room = 21; start = 90; tau = 12;
t = 0:5:30;
T = room + (start - room) * exp(-t / tau);
fprintf('t = %2d min: %5.1f C\n', [t; T]);t = 0 min: 90.0 C
t = 5 min: 66.5 C
t = 10 min: 51.0 C
t = 15 min: 40.8 C
t = 20 min: 34.0 C
t = 25 min: 29.6 C
t = 30 min: 26.7 C
If you prefer a true notebook, Jupyter has an Octave kernel (octave_kernel), and MATLAB has an official Jupyter integration as well. Live scripts can also be exported to .ipynb.
Apps and callbacks
A graphical app is event-driven. Instead of running top to bottom, your code sets up the window and then waits; when the user clicks a button or types a value, MATLAB calls the function you attached to that event, the callback. Every callback receives two arguments: the component that fired (src) and an event object (event) with details such as the new value.
A programmatic app with uifigure
Modern MATLAB GUIs are built from uifigure and its components (uibutton, uieditfield, uidropdown, uislider, uiaxes, uitable, ...), arranged by hand or with uigridlayout. This complete converter runs in MATLAB:
fig = uifigure('Name', 'Converter', 'Position', [100 100 320 120]);
g = uigridlayout(fig, [2 2]);
uilabel(g, 'Text', 'Celsius');
c = uieditfield(g, 'numeric', 'Value', 20);
uilabel(g, 'Text', 'Fahrenheit');
f = uilabel(g, 'Text', '68.0');
c.ValueChangedFcn = @(src, event) set(f, 'Text', sprintf('%.1f', event.Value * 9/5 + 32));Typing 100 into the field fires ValueChangedFcn with event.Value equal to 100 (and event.PreviousValue equal to 20), and the label changes to 212.0. Octave has a uifigure function, but it creates a classic figure; the newer components (uieditfield, uibutton, uidropdown, uigridlayout, ...) are MATLAB-only. Octave does have the older uicontrol, uipanel, uitable and uimenu.
App Designer
App Designer (appdesigner at the command line) lets you lay out components by drag and drop, then writes a class for you. The generated code is ordinary object-oriented MATLAB (see Classes (classdef)): the components are properties, and each callback is a method that receives app plus the event:
classdef ConverterApp < matlab.apps.AppBase
properties (Access = public)
UIFigure matlab.ui.Figure
CelsiusField matlab.ui.control.NumericEditField
UnitDropDown matlab.ui.control.DropDown
ResultLabel matlab.ui.control.Label
end
methods (Access = private)
function startupFcn(app)
updateResult(app);
end
function CelsiusFieldValueChanged(app, event)
updateResult(app);
end
function updateResult(app)
c = app.CelsiusField.Value;
if strcmp(app.UnitDropDown.Value, 'Kelvin')
app.ResultLabel.Text = sprintf('%.2f K', c + 273.15);
else
app.ResultLabel.Text = sprintf('%.1f F', c * 9/5 + 32);
end
end
end
% ... generated createComponents(app), constructor and delete method
endYou only write the bodies of the callbacks and helper methods; App Designer maintains the layout code (shown greyed out in its editor). The app runs by calling its name, app = ConverterApp;, and can be shared as an installer (.mlappinstall), compiled into a standalone program with MATLAB Compiler, or deployed as a web app.
Simulating callbacks (runnable)
The structure of an App Designer app doesn't depend on the windows. The class below keeps the same shape, with components replaced by structs that hold a Value or Text, so you can see how state, callbacks and a shared update method interact. Calling a callback with a hand-made event struct is also how you test app logic without clicking:
app = ConverterApp(); % the constructor plays startupFcn
% the user types 100 ...
app.CelsiusField.Value = 100;
app.CelsiusFieldValueChanged(struct('Value', 100, 'PreviousValue', 20));
% ... switches the unit ...
app.UnitDropDown.Value = 'Kelvin';
app.UnitDropDownValueChanged(struct('Value', 'Kelvin'));
% ... and types an impossible temperature
app.CelsiusField.Value = -300;
app.CelsiusFieldValueChanged(struct('Value', -300, 'PreviousValue', 100));
fprintf('result updated %d times\n', app.Updates);
% --- file: ConverterApp.m
classdef ConverterApp < handle
% Stand-in for an App Designer class: struct "components", same callbacks.
properties
CelsiusField = struct('Value', 20)
UnitDropDown = struct('Value', 'Fahrenheit')
ResultLabel = struct('Text', '')
Updates = 0
end
methods
function app = ConverterApp()
app.updateResult();
end
function CelsiusFieldValueChanged(app, event)
if event.Value < -273.15
app.CelsiusField.Value = event.PreviousValue; % reject the input
app.ResultLabel.Text = 'below absolute zero, reverted';
app.show();
return
end
app.updateResult();
end
function UnitDropDownValueChanged(app, event)
app.updateResult();
end
end
methods (Access = private)
function updateResult(app)
c = app.CelsiusField.Value;
switch app.UnitDropDown.Value
case 'Fahrenheit', app.ResultLabel.Text = sprintf('%.1f F', c * 9/5 + 32);
case 'Kelvin', app.ResultLabel.Text = sprintf('%.2f K', c + 273.15);
end
app.Updates = app.Updates + 1;
app.show();
end
function show(app)
fprintf('[%6.1f C] [%-10s] -> %s\n', app.CelsiusField.Value, ...
app.UnitDropDown.Value, app.ResultLabel.Text);
end
end
end[ 20.0 C] [Fahrenheit] -> 68.0 F
[ 100.0 C] [Fahrenheit] -> 212.0 F
[ 100.0 C] [Kelvin ] -> 373.15 K
[ 100.0 C] [Kelvin ] -> below absolute zero, reverted
result updated 3 times
Because the class derives from handle, every callback modifies the same app object, which is exactly why App Designer apps are handle classes (see Handle Classes & Events).
Classic figures with uicontrol (MATLAB and Octave)
Before uifigure, GUIs were built with figure and uicontrol. This style still works in MATLAB and is the one Octave supports (with its Qt graphics), so it is the choice for a GUI that must run in both. Shared state is usually stored on the figure with guidata:
f = figure('Name', 'Counter', 'MenuBar', 'none', 'Position', [200 200 260 100]);
data.count = 0;
data.label = uicontrol(f, 'Style', 'text', 'String', '0', 'Position', [20 50 220 30]);
guidata(f, data);
uicontrol(f, 'Style', 'pushbutton', 'String', 'Add one', ...
'Position', [20 10 100 30], 'Callback', @onAdd);
function onAdd(src, ~)
data = guidata(src); % fetch the state
data.count = data.count + 1;
set(data.label, 'String', sprintf('%d', data.count));
guidata(src, data); % store it back
endThe same fetch-update-store cycle, with a struct in place of the figure, runs anywhere:
state = struct('count', 0, 'log', {{}});
clicks = {'add', 'add', 'reset', 'add'};
for k = 1:numel(clicks)
src.Tag = clicks{k}; % which button fired
state = onClick(state, src, []);
end
fprintf('final count: %d\n', state.count);
fprintf('log: %s\n', strjoin(state.log, ' -> '));
function state = onClick(state, src, ~)
switch src.Tag
case 'add', state.count = state.count + 1;
case 'reset', state.count = 0;
end
state.log{end + 1} = sprintf('%s(%d)', src.Tag, state.count);
endfinal count: 1
log: add(1) -> add(2) -> reset(0) -> add(1)
Tip: Keep the real work in ordinary functions and make callbacks thin wrappers that read inputs, call those functions and display results. The functions stay testable (see Unit Testing), and switching between App Designer,
uicontroland a script costs almost nothing.
Learn more: Function Handles & Callbacks, Handle Classes & Events, Saving & Exporting Figures.
Test yourself
Which two arguments does a component callback such as ValueChangedFcn receive?
- The figure and the axes
- The source component and an event object
- The old and new value as numbers
- None
What does App Designer generate from your layout?
- A live script
- A MEX file
- A classdef class whose properties are the components and whose methods are the callbacks
- A Simulink model
Which option works in both MATLAB and Octave for documenting a script with its results?
- Live Editor (.mlx)
- Sections with %% plus publish
- App Designer
- Live tasks