- Undocumented Matlab - https://undocumentedmatlab.com -

Changing Matlab's Command Window colors – part 2

Posted By Yair Altman On May 9, 2009 | 9 Comments

Following my post on Changing Matlab’s Command Window colors [1], I received an email asking whether it was possible to temporarily set the Command-Window (CW)’s text color, so that subsequent disp or fprintf commands will be displayed in some specific text color.
The short and simple answer is no: You can change the text color as described in that post, but once you modify it, the entire CW text will be modified accordingly.
Unfortunately, CW (like Swing’s standard JTextArea [2] of which CW is an instance) does not automatically support HTML formatting as most other uicontrols [3]. In fact, CW’s default Document object, which holds all the text-area’s text and font style information, is an extension of Swing’s javax.swing.text.PlainDocument [4], which does not allow any text style formatting. And the CW object itself is a simple JTextArea, which does not enable using a styled Document object. Perhaps in a future version MathWorks would be willing to use the almost identical (syntactically-wise) JTextPane [5], which does enable styled text runs.
To a very limited degree, you can use the fact that hyperlinks and error messages have separate styles than standard text (respectively, blue underlined and red). So, you can output text in these styles as follows:

% Use hyperlink style:
>> disp('<a href="">my text</a>')
my text
% Use STDERR (fid=2) style:
>> fprintf(2,'my text\n')
my text

Note: you could possibly get a handle to the JTextArea (see the previous post) and then set its UI property to your tailor-made Java UI class. In this class you can set text segment properties based on whichever properties you wish. This is apparently how Matlab overcame JTextPane’s limitations: they implemented a custom com.mathworks.mde.cmdwin.CmdWinSyntaxUI class that extends javax.swing.plaf.basic.BasicTextAreaUI. But implementing such a custom UI class while retaining Matlab’s syntax-highlighting functionality is most probably very difficult and not worth the effort. So we’ll just wait for some supported method in a future Matlab release.
Addendum (May 13, 2009): I have found a way to enable display of text in any color, as well as underline, in the Command Window. See my post on cprintf [6] for details.

Categories: Desktop, Low risk of breaking in future versions


9 Comments (Open | Close)

9 Comments To "Changing Matlab's Command Window colors – part 2"

#1 Comment By Ken Orr On May 11, 2009 @ 04:51

Hi Yair,

Clever use of the tools at hand! We would definitely like to allow users to change the color of their text in the Command Window (this is a frequent request). We’re thinking about more robust highlighting mechanisms now.

Depending on your needs, [13] could also serve as an intermediate solution, as you could embed HTML into your output, which would then be rendered by a web browser.

-Ken

#2 Pingback By cprintf – display formatted color text in the Command Window | Undocumented Matlab On May 16, 2009 @ 10:46

[…] In earlier posts I showed how to modify the Command Window (CW) text and background color, and a very limited method of displaying red (error) and blue (hyperlinked) CW messages. Since then, I was obsessed with finding a better way to CW display text in any color. After lots […]

#3 Comment By Malcolm Lidierth On October 9, 2011 @ 09:35

N.B. href=””
can be used add a matlab command to run when the text is clicked
href=”matlab:disp(‘Command replaces disp(….) etc’)”

#4 Comment By Rohan On October 10, 2011 @ 07:35

I am Beginner to MATLAB. By using the command NET.addAssembly i am loading .NET Assembly. Instantiating the object of Assembly’s class i am invoking the methods of the class.

Passing the parameter such as double, char to method of assembly class its working fine.

But when i am trying to pass cell array to method of instantiated class its showing the error parameter mismatch.

i have done following procedure

s=NET.addAssembly('name of assembly')
t = s.AssemblyHandle.GetType('Class present in assembly');
obj = System.Activator.CreateInstance(t);
obj.PassCellArray(CellArray);

.NET MEthod

public void PassCellArray(System.Object[] dd) {}

As reference from MATHWORKS, using this object passing the Cell array to method which has parameter as System.Object[].

So please help me for how to pass cell array to .NET method.

#5 Comment By Yair Altman On October 10, 2011 @ 08:24

@Rohan – Try converting your cell array into a regular array; also ensure that your array contents can be directly converted into System.Object.

A better place for this and similar questions would be on [14] or the [15] or to directly ask MathWorks technical support. This website is not a general Matlab Q&A forum…

#6 Pingback By Monsters Inc – Octave:Улучшаем качество сценариев и не только On March 19, 2012 @ 04:33

[…] Маленькое замечание , возможно кто нибудь захочет сделать часть вывода цветным. Пока такая возможность не доступна как и не одступен пока описанная в документации поддержка echo (octave -x). Почему нельзя делать цветной вывод, максимум что вы можете сделать цветным это приглашение вашего терминала PS1 в файле .octaverc . Возможно (не проверял)также изменить цвет для вывода сообщений об ощибках, как это сделано в qtOctave и использовать в некоторых функциях, вывод, как вывод ошибки, подробнее […]

#7 Pingback By Octave: Улучшаем сценарии | Crafting blog On March 27, 2013 @ 04:29

[…] Маленькое замечание , возможно кто нибудь захочет сделать часть вывода цветным. Пока такая возможность не доступна как и не одступен пока описанная в документации поддержка echo (octave -x). Почему нельзя делать цветной вывод, максимум что вы можете сделать цветным это приглашение вашего терминала PS1 в файле .octaverc . Возможно (не проверял)также изменить цвет для вывода сообщений об ощибках, как это сделано в qtOctave и использовать в некоторых функциях, вывод, как вывод ошибки, подробнее […]

#8 Pingback By Заметки по Octave | Crafting.be On September 6, 2013 @ 16:41

[…] Почему нельзя делать цветной вывод, максимум что вы можете сделать цветным это приглашение вашего терминала PS1 в файле .octaverc . Возможно (не проверял)также изменить цвет для вывода сообщений об ошибках, как это сделано в qtOctave и использовать в некоторых функциях, вывод, как вывод ошибки, подробнее […]

#9 Pingback By Another Command Window text color hack | Undocumented Matlab On November 6, 2013 @ 11:02

[…] built-in fprintf or disp commands. Back in 2009, I explained the undocumented ability to display red or hyperlinked text using fprintf, or colored and underlined text using my cprintf utility. I followed this up […]


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors-part2

URLs in this post:

[1] Changing Matlab’s Command Window colors: http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/

[2] JTextArea: http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html

[3] HTML formatting as most other uicontrols: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/

[4] javax.swing.text.PlainDocument: http://java.sun.com/javase/6/docs/api/javax/swing/text/PlainDocument.html

[5] JTextPane: http://java.sun.com/docs/books/tutorial/uiswing/components/text.html

[6] cprintf: http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/

[7] Changing Matlab's Command Window colors : https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors

[8] Command Window text manipulation : https://undocumentedmatlab.com/articles/command-window-text-manipulation

[9] EditorMacro v2 – setting Command Window key-bindings : https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings

[10] Bold color text in the Command Window : https://undocumentedmatlab.com/articles/bold-color-text-in-the-command-window

[11] Another Command Window text color hack : https://undocumentedmatlab.com/articles/another-command-window-text-color-hack

[12] cprintf – display formatted color text in the Command Window : https://undocumentedmatlab.com/articles/cprintf-display-formatted-color-text-in-command-window

[13] : http://www.mathworks.com/help/techdoc/matlab_env/f6-22451.html

[14] : http://www.StackOverflow.com

[15] : https://www.mathworks.com/matlabcentral/newsreader/

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.