How to add a date or timestamp to the Matlab prompt? [duplicate]

扶醉桌前 提交于 2019-12-10 11:36:39

问题


This question has been marked as a duplicate of another question asking about the possibility of displaying the current system hostname in the prompt. The accepted answer to that question successfully uses the same custom prompt, called setPrompt(), as does the accepted answer to this question (see below).

Please note however: that question does not talk about date/timestamps, nor does the answer give detailed step-for-step instructions. The answer below does both of those things and might, or might not, therefore be the best-suited answer for you.

It can be useful to know when (e.g. "11:32 pm") a Matlab command has been executed. I have been looking for the possibility to show a date/timestamp in the Matlab prompt. I am using Matlab R2017b on Windows 10.

Is that possible, and if so, what are the step-for-step instructions for implementing such a prompt?

Preferably I want it to look like the following:

[13:45:57] >> 1
ans =
     1
[13:45:58] >>

Note: It turns out that it is indeed possible, check the answer (self-written).


回答1:


As of the R2017b release, it is possible to display command execution timestamps using setPrompt, a custom command prompt from the Matlab File Exchange (see instructions below).

For a technical explanation of the code, see the author's original blog post about setPrompt.


Configuring a custom prompt

Configuring a custom prompt is easy and takes only a few minutes.

  • Download and extract setPrompt.m into your Matlab path (find it with the userpath command).
  • If you want to set the prompt permanently, you can put a call to setPrompt() in your startup.m file.
  • If you haven't configured your startup file, simply go to the Matlab command line and do:

    1. userpath

    2. cd <YOUR_USERPATH>, (usually C:\Users\<USER>\Documents\MATLAB).

    3. edit startup.m, click "yes", and add your setPrompt call into the file.

As an example, you can put setPrompt('<timestamp> ') in your startup file to get a continuously updated date/timestamp prompt. To get back the default prompt, call setPrompt without arguments.

>> setPrompt('<timestamp> ')
<06-Feb-2018 01:00:51> 
<06-Feb-2018 01:00:53> setPrompt()
>>

To get exactly what I wanted I used setPrompt('[''['',datestr(now, ''HH:MM:SS''), ''] >> '']')

>>
>> setPrompt('[''['',datestr(now, ''HH:MM:SS''), ''] >> '']')
[13:45:57] >>
[13:45:57] >> 1

ans =

     1

[13:45:58] >>


来源:https://stackoverflow.com/questions/48646435/how-to-add-a-date-or-timestamp-to-the-matlab-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!