Skip to content

MATLAB - Editors

MATLAB provides two powerful, integrated environments for writing and executing code: the classic MATLAB Editor for traditional software development, and the interactive Live Editor for exploratory analysis and literate programming. Understanding the strengths of each is key to being a productive MATLAB user.

The MATLAB Editor is a full-featured Integrated Development Environment (IDE) optimized for writing scripts, functions, and classes. You can open it by typing edit or edit <filename>.m in the Command Window, or by clicking the New Script button on the HOME tab.

  • Code Analyzer: The Editor continuously checks your code for errors, potential bugs, and bad practices. Look for the colored markers in the indicator bar on the right: a red marker indicates a syntax error that will prevent your code from running, while an orange marker indicates a warning or a suggestion for improvement. Hovering over a marker provides a detailed explanation.
  • Code Sections (%%): You can divide your script into independent, runnable sections by starting a line with %%. This is incredibly useful for developing code incrementally. Use the Run Section button to execute only the code in the current section.
  • Tab Completion and Suggestions: As you type, the Editor suggests function names, variables, and arguments, reducing typos and helping you discover syntax.
  • Code Refactoring: You can automatically rename a variable or function across an entire file or project, ensuring consistency.

The Editor includes a powerful visual debugger, which is essential for finding and fixing problems in your code. Here’s a basic workflow:

  1. Set a Breakpoint: Click in the gray margin to the left of a line number. A red dot will appear. When you run your code, execution will pause before this line is executed.
  2. Run the Code: Run your script or function as usual. It will stop at the breakpoint, and the command prompt will change to K>> to indicate you are in debug mode.
  3. Inspect Variables: Hover your mouse over variables in the Editor to see their current values, or look in the Workspace browser.
  4. Step Through Code: Use the debugging controls on the Editor tab:
    • Step (F10): Execute the current line and move to the next.
    • Step In (F11): If the current line is a function call, move into that function to debug it line by line.
    • Step Out (Shift+F11): If you are inside a function, finish executing it and return to the line where it was called.
  5. Continue: Click Continue to resume normal execution until the next breakpoint or the end of the program.

The Live Editor is a revolutionary environment for creating ‘live scripts’—interactive documents that weave together code, its output (including plots and tables), and richly formatted text. This approach, known as literate programming, is ideal for exploration, sharing results, and teaching.

To create a live script, click the New Live Script button on the HOME tab. Live scripts are saved with a .mlx extension.

  • Inline Output: When you run a section of code, the output, plots, and tables appear directly below the code that generated them, creating a cohesive narrative.
  • Rich Text and Equations: Switch from a code block to a text block to add headings, bullet points, images, hyperlinks, and even beautifully rendered LaTeX equations to explain your work.
  • Interactive Controls: Add sliders, drop-down menus, and buttons to your live script to create simple interactive dashboards. This allows you or others to easily change parameters and re-run the analysis without editing the code.
  • Sharing: Export your live script as a PDF, HTML, or Word document. You can choose to hide the code to produce a clean, professional-looking report.

In a live script, you can create a slider to control a plot’s frequency:

%% Interactive Sine Wave Plot
% Use the slider to change the frequency of the wave.
% Add a numeric slider control from the LIVE EDITOR tab
% Set its range from 1 to 10. Let's assume it's linked to a variable `frequency`.
frequency = 5; % This value will be updated by the slider
x = 0:0.01:2*pi;
y = sin(frequency * x);
plot(x, y);
title(['Sine Wave with Frequency: ', num2str(frequency), ' Hz']);
ylim([-1.1, 1.1]);

When you run this script, a slider appears. Dragging the slider automatically re-runs the code, updating the plot in real-time.

In a text block in the Live Editor, you can use these shortcuts:

Text StyleAuto-FormattingKeyboard Shortcut
Title# text + EnterCtrl + Alt + L
Heading 1## text + EnterCtrl + Shift + 1
Bullets* text + EnterCtrl + Alt + U
Bold**text** or __text__Ctrl + B
Italic*text* or _text_Ctrl + I
Codetext or |text|Ctrl + Alt + T

Editor vs. Live Editor: When to Use Which?

Section titled “Editor vs. Live Editor: When to Use Which?”
  • Use the Live Editor (.mlx) for:
    • Exploratory Analysis: Experimenting with data and visualizing results iteratively.
    • Reports and Documentation: Creating rich documents that combine your analysis with explanations.
    • Teaching and Homework: Clearly explaining steps and showing results in an educational context.
    • Simple Interactive Tools: Building quick dashboards with interactive controls.