如何在 Matlab 提示中添加日期或时间戳?

How to add a date or timestamp to the Matlab prompt?

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.

了解何时(例如“11:32 pm”)执行了 Matlab 命令会很有用。我一直在寻找在 Matlab 提示符中显示 date/timestamp 的可能性。我在 Windows 10.

上使用 Matlab R2017b

这是否可能?如果可能,实施此类提示的分步说明是什么?

我希望它看起来像下面这样:

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

注:事实证明确实可以,查看答案(自己写的)。

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.


配置自定义提示

配置自定义提示很容易,只需几分钟。

  • 下载 setPrompt.m 并将其解压到您的 Matlab path 中(使用 userpath 命令找到它)。
  • 如果您想永久设置提示,可以在 startup.m 文件中调用 setPrompt()
  • 如果您还没有配置启动文件,只需转到 Matlab 命令行并执行以下操作:

    1. userpath

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

    3. edit startup.m,单击 "yes",然后将您的 setPrompt 调用添加到文件中。

例如,您可以将 setPrompt('<timestamp> ') 放入启动文件中以获得持续更新的 date/timestamp 提示。要恢复默认提示,请调用不带参数的 setPrompt。

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

为了得到我想要的,我使用了setPrompt('[''['',datestr(now, ''HH:MM:SS''), ''] >> '']')

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

ans =

     1

[13:45:58] >>