MATLAB Answers

Color coding commented statements and comments following a statement

120 views (last 30 days)
PChoppala on 18 Nov 2015
Commented: Matias Oddo on 13 May 2020
I would like to know if there is a possibility of having a different color for a commented statement and a comment that follows a statement, e.g.,
% u=rand
u = rand % generate a random number from U[0,1]
shows both in green but I would like to differentiate. I could not find a lead from MATLAB preferences.


Show 2 older comments
John D'Errico
John D'Errico on 10 Nov 2019
Stephen makes a good point, of a case where it would be useful to explicitly set comment color differently on demand.
For example, when I am writing code that will require several days or more to write, I often insert a garbage line in the code where I stop for the day, just random characters on the keyboard.
This way, if I tried to excute the code, it will fail, and it is obvious where I left off. Better would be to have a comment in RED perhaps, that would be a strong visual flag to draw my eye to the spot. And since it is a comment, I can note exactly what needs to be done in that missing block of code.
I can even see multi-colored comments as useful when code is shared between multiple people who are co-authoring the code.
But personally, I can see essentially no purpose in having MATLAB automatically differentiate comment color based on where the comment lies, thus an inline comment versus a stand alone comment.
Steven Lord
Steven Lord on 11 Nov 2019
That seems like a reasonable use case for having comments in multiple colors. Right now all comments are a single color, though you can change that color in the Preferences. I recommend sending this to Technical Support as an enhancement request, so it gets captured in the enhancement database.
There is some functionality that may help you identify work that needs to be done (by who needs to do that work or for which step of your project the work needs to be completed.) The TODO/FIXME report available from the Current Folder browser will let you search for specific identifiers (TODO, FIXME, or "Fix:Stephen".)
If you're going to leave the file open in your MATLAB session but need a "pick up from here" marker (say if you're going home for the night) you can set bookmarks in the file (see the "Set Bookmarks" item under the "Go To Location in File" section on that page.)
Matias Oddo
Matias Oddo on 13 May 2020
Commenting to follow this thread.
Perhaps the # symbol could work as a clone of % but in a different color. That would be simple and mighty useful!

Sign in to comment.

Answers (3)

Michal Demjan
Michal Demjan on 21 Aug 2019
Also interested!
Maybe just implement different commenting function starting with '%!' or something..


Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by