Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Comment in MATLAB: Single-Line, Block and Shortcut

Comment in MATLAB with %, block comments with %{ %}, the Editor Comment button and shortcuts, plus help text for shared research scripts.

Written and maintained by CASRAI Editorial Board

Last updated

To comment in MATLAB, start a line with the percent sign (%). MATLAB ignores everything after the % on that line. To comment several lines at once, wrap them in %{ and %} on their own lines, or select the lines in the Editor and use the Comment button or its keyboard shortcut. This guide is for researchers and analysts who want clear, shareable analysis scripts.

What comments are used for in research code

MATLAB’s documentation says to add comments to code with the percent symbol, either on a line of its own or at the end of a line of code. In research work, comments do three jobs: they explain why a step exists (for a collaborator or a reviewer), they switch code off temporarily while debugging, and, when placed at the top of a file, they become the help text that MATLAB prints for that program. Well-commented scripts are one small part of reproducible research practices and make code review of research software easier.

Which comment syntax works where

Syntax What it does Rule from the MathWorks documentation
% Comments out the rest of the line Can start a line or follow code on the same line
%{ ... %} Comments out a block of lines The %{ and %} operators must appear alone on their lines, with no other text on those lines
... (ellipsis) Continues a statement on the next line Use it instead of % to comment part of a multi-line statement
%% Starts a code section; the text after it is the section title Section titles are comments that begin with %%
Comment block at the top of a file Becomes the help text shown by help For a function, place it directly below the function definition line (or below the arguments block)

Single-line comments: comment in MATLAB with %

Comment in MATLAB at the start of a line

Type % as the first character, then your text. The whole line is ignored when the script runs.

% Load the raw measurements
data = readmatrix('trial1.csv');

MATLAB comments at the end of a line

Place % after a statement to annotate it without disabling it.

threshold = 0.05;   % significance level used in all tests

Block comments: comments in MATLAB across many lines

Put %{ on a line by itself, then the lines to disable, then %} on a line by itself.

%{
data = readmatrix('trial2.csv');
summary(data)
%}

The documentation states that the block operators must appear alone on their lines. Do not put text, including a note such as %{ old analysis, on the same line as %{ or %}; keep any explanation on a separate % line above the block.

Comment and uncomment with the Editor button or a shortcut

  1. Open the file in the MATLAB Editor or the Live Editor.
  2. Select the lines you want to comment.
  3. In the Code section, click Comment. To reverse it, select the same lines and click Uncomment.

The MathWorks comments page lists keyboard shortcuts for the same actions: on Windows, Ctrl+R comments the selection; on Linux, Ctrl+/ comments and Ctrl+Shift+/ uncomments; on macOS, Command+/ comments and Command+Option+/ uncomments. Sources differ on the Windows uncomment shortcut (one MathWorks Answers reply gives Ctrl+T for the Windows default set), so check your own binding as described below.

If the comment shortcut does nothing

MathWorks Answers advises checking the active binding: go to the Home tab, open Preferences (Environment), then MATLAB > Keyboard > Shortcuts, and search for the action “Comment” to see which shortcut is assigned. You can also select the Windows Default Set there. Another application may be intercepting the key combination, so test it with the other program closed.

Comment out part of a multi-line statement

A % comment runs to the end of its line. The documentation says to use the ellipsis (...) to continue a statement across lines, in place of %, when you want to comment part of a multi-line statement.

Section titles and help text

A line beginning with %% starts a section, and its text is the section title. Comments placed at the start of a program also serve as help text. For a function, that block goes directly below the function definition line. Its first line is the H1 line: the program name plus a brief description, which MATLAB uses in the Files panel, in help, and in lookfor. Running help followed by the program name prints that comment block; comment lines after the help block are ignored by help. For lab scripts that others will reuse, a clear H1 line is the cheapest documentation you can add.

Troubleshooting

  • A block comment is not working. Check that %{ and %} are each alone on their line, with nothing else, per the documentation.
  • The shortcut does nothing. Check Preferences > Keyboard > Shortcuts as above, and close other programs that may capture the key combination.
  • Help prints the wrong text. Help text must be the comment block at the start of the program, directly under the function definition line for functions.

Free alternative: GNU Octave

Tool Single-line comment character Source
MATLAB % MathWorks documentation
GNU Octave # or %; text after it is ignored Octave manual, Single-Line Comments

Because Octave accepts % as well, simple single-line comments written for MATLAB use a character Octave also treats as a comment.

Frequently asked questions

How do you comment in MATLAB?

Start the line with %. Everything after it on that line is ignored. For several lines, use %{ and %} on their own lines or the Editor’s Comment button.

How do you write a block comment in MATLAB?

Put %{ alone on one line, your lines below it, and %} alone on the last line. No other text may share those two lines.

What is the shortcut for comments in MATLAB?

On Windows the documented comment shortcut is Ctrl+R; on Linux it is Ctrl+/ and on macOS Command+/. Confirm the binding in Preferences > Keyboard > Shortcuts.

How do you comment part of a multi-line statement?

Use the ellipsis (...) to continue the statement instead of %, as the documentation describes.

Related reading: MATLAB coding: scripts, functions and a first program, MATLAB code, MATLAB if statements and MATLAB documentation.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Comment in MATLAB: Single-Line, Block and Shortcut

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →