Changing Matlab’s Command Window colors – part 2

Following my post on Changing Matlab’s Command Window colors, 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 of which CW is an instance) does not automatically support HTML formatting as most other uicontrols. 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, 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, 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 for details.

Categories: Desktop, Low risk of breaking in future versions

Tags:

Bookmark and SharePrint Print

9 Responses to Changing Matlab’s Command Window colors – part 2

  1. Ken Orr says:

    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, publishing 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: cprintf - display formatted color text in the Command Window | Undocumented Matlab

  3. Malcolm Lidierth says:

    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. Rohan says:

    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.

    • @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 StackOverflow or the CSSM newsgroup or to directly ask MathWorks technical support. This website is not a general Matlab Q&A forum…

  5. Pingback: Monsters Inc - Octave:Улучшаем качество сценариев и не только

  6. Pingback: Octave: Улучшаем сценарии | Crafting blog

  7. Pingback: Заметки по Octave | Crafting.be

  8. Pingback: Another Command Window text color hack | Undocumented Matlab

Leave a Reply

Your email address will not be published. Required fields are marked *