Code covered by the BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
setPrompt - Set the Command Window prompt

4.9 | 11 ratings Rate this file 18 Downloads (last 30 days) File Size: 11.9 KB File ID: #26471 Version: 1.4
image thumbnail

setPrompt - Set the Command Window prompt


Yair Altman (view profile)


24 Jan 2010 (Updated )

Sets the Command Window prompt to the specified string

| Watch this File

File Information

setPrompt(newPrompt) sets the Command Window prompt to the specified NEWPROMPT.
NEWPROMPT can be one of the following:
       - a static string: setPrompt('>> ')
              => this is the default prompt string ('>> ')
       - an evaluable string: setPrompt('datestr(now)')
              => the new prompt will look like: '25-Jan-2010 01:00:51'
              Note: the evaluable string is expected to return a string
       - an evaluable function: setPrompt(@()(['[',datestr(now),'] ']))
              => the new prompt will look like: '[25-Jan-2010 01:00:51] '
              Note: the evaluable function is expected to return a string
       - the static string 'timestamp' will continuously update the last (current)
         prompt with the current time: '[25-Jan-2010 01:00:51] '. This has
         the effect of displaying desktop command execution times.
         The 'timestamp' string can be used with other static text to
         customize its appearance. For example: setPrompt('<timestamp!> ').
       - an empty value or no input argument restores the default prompt
     setPrompt('[-]') % Replaces '>> ' prompt with '[-]'
     setPrompt('%') % => '% ' (space-padded)
     setPrompt('sprintf(''<%f>'',now)') % => '<734163.056262>'
     setPrompt('datestr(now)') % => '25-Jan-2010 01:00:51' (unchanging)
     setPrompt('[''['',datestr(now),''] '']') % => '[25-Jan-2010 01:00:51] '
     setPrompt(@()(['[',datestr(now),'] '])) % => '[25-Jan-2010 01:00:51] '
         (note that these are the same: the first uses an evaluable string,
          while the second uses an evaluable function)
     setPrompt('timestamp') % => '[25-Jan-2010 01:00:51] ' (continuously-updated)
     setPrompt('<timestamp> ') % => '<25-Jan-2010 01:00:51> ' (continuously-updated)
     setPrompt('>> ') % restores the default prompt
     setPrompt('') % restores the default prompt
     setPrompt % restores the default prompt
     This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7+, but use at your own risk!
Technical explanation:
     A technical explanation of the code in this utility can be found on
Bugs and suggestions:
     Please send to Yair Altman (altmany at gmail dot com)


Cprintf Display Formatted Colored Text In The Command Window inspired this file.

MATLAB release MATLAB 7.6 (R2008a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
22 Apr 2016 Jonathan

I put setPrompt in my startup.m. It was causing "maxrhs: too many arguments error". I traced it to the jTextArea assignment at about line 96. It was trying to operate before the commandwindow was created. So I modified to this block that keeps retrying:

cmdWin = jDesktop.getClient('Command Window');
jTextArea = cmdWin.getComponent(0).getViewport.getComponent(0);
jTextArea = [];
while isempty(jTextArea)
disp('setPrompt: try getting jTextArea again');
jTextArea = jDesktop.getMainFrame.getFocusOwner;

10 Oct 2014 Yair Altman

Yair Altman (view profile)

@Qun HAN - I've updated the utility for R2014a & R2014b. Download the latest version.

Comment only
10 Oct 2014 Qun HAN

Qun HAN (view profile)

In MATLAB R2014A, I got the following error.

Error using setPrompt (line 115)
The name 'CaretUpdateCallback' is not an accessible property for an instance of class 'com.mathworks.mde.cmdwin.XCmdWndView'

Comment only
28 Jul 2013 Laurent

This is what I was looking for
Thx Yair

04 Jul 2013 Markus Leuthold

Hi Yair

Great submission! Two questions

1) I've put setPrompt into startup.m. However, it doesn't work from there. I'm sure setPrompt is executed (added a disp statement to verify). It works fine AFTER Matlab is started

2) Any idea how to change the caret color? I've added
set(jTextArea,'caretColor',[1 0 0]);
to your code, but it doesn't seem to work. Neither does your uiinspect work on the property 'caretColor'


24 Jan 2013 Gary

Gary (view profile)

13 Sep 2012 Skirmante

really useful function, only one request. Could you look into debugging while using setPromt? If you debug step by step, it prints new setPromt message appending last one and deleting few symbols from last prompt. It looks like that:
'[2012-09-13 11:36:[2012-09-13 11:36:[2012-09-13 11:36:[2012-09-13 11:36:13]'
I could do that myself but that would take time to understand this code :) thanks

22 May 2012 Brian

Brian (view profile)

19 Feb 2011 Kenneth Eaton

Kenneth Eaton (view profile)

Very nice! The only problem I had when trying to run it on R2010b was this warning message:

Warning: Possible deprecated use of set on a Java callback.
> In setPrompt at 115

Which I suppressed by doing the following:


11 Jan 2011 Andy

Andy (view profile)

I have a student edition of ML R2010a on Windows 7. It comes with the default prompt 'EDU>>', which I wanted to change back to the familiar '>>'. setPrompt worked, but it leaves too much space (copied and pasted):

EDU>> setPrompt('>>');
>> see how much space there is?

I would have preferred:

EDU>> setPrompt('>>');
>> only one space before text

Still, great stuff. Thanks, Yair!

01 Feb 2010 Jean-Guilhem Dalissier

Thank you very much Yair, your new version works perfectly. Tested successfully on R2008b and R2007b. Work also perfectly on R14SP3 adding a “try, if ismac … end”.

27 Jan 2010 John D'Errico

John D'Errico (view profile)

I would only add (as one of the people who reported a Mac problem) that I saw the problem in release R2007b, on an old mac with probably an old copy of Java. For the rest of you, rest assured that this appears to be a well written, well documented, professional piece of code.

Comment only
27 Jan 2010 Yair Altman

Yair Altman (view profile)

*** Mac users ***
Please note that I have received information that setPrompt causes problems on MacOS. It appears to work ok on non-Mac platforms though (inc. Windows)

Comment only
26 Jan 2010 Yair Altman

Yair Altman (view profile)

oops - temporary problem with the webpage (now fixed)...

I don't know the legal status of removing the EDU from the prompt. Since this is the first time anyone has been able to modify the prompt, I think the question has never before arisen.

Comment only
26 Jan 2010 Jan Simon

Jan Simon (view profile)

I cannot get the link to the blog on undocumentedmatlab to run (-> 404 error).
Is there a statement from TMW, if removing "EDU" from the prompt conclicts with the license conditions? See:
If it is legal, I love it.

Comment only
26 Jan 2010 Aurelien Queffurust

I thought that it was impossible to modify this "core" feature. tested successfully under R2010a Prerelease. Great job Yair!

25 Jan 2010 Ragaar Ashnod  
26 Jan 2010 1.1

Fixed a few edge cases (some inspired by J. Raymond); added continuously-updated timestamp option

29 Jan 2010 1.2

Fixed a few edge-cases (some reported by J.G. Dalissier) with '>> ' terminated prompts; fixed a few problems with continuous timestamps; enabled customizing continuous timestamp prompts; added Mac warning

10 Oct 2014 1.3

Fixed for R2014a & R2014b

20 Oct 2014 1.4

Updated submission title (no code change)

Contact us