Shortcut to rerun last command

47 次查看(过去 30 天)
Is it there a way to setup a shortcut to rerun the last command typed in the command window?
So a shortcut that does the equivalent of the following set of operations but without the need to focus the command window: - Type up arrow key in command window to retrieve the last command - Hit enter to run hit

回答(3 个)

Cam Salzberger
Cam Salzberger 2015-9-4
编辑:Cam Salzberger 2015-9-4
Hello Simone,
I understand that you would like to be able to rerun the last command entered in the command window without needing to focus on the command window. Unfortunately, there don't appear to be any keyboard shortcut options that will do that, or even ones to bring the focus to the command window. You could double-click the command in the Command History (or highlight multiple commands and hit Enter), but I imagine that's not quite what you want either.
You can create a custom shortcut button which can appear in the Quick Access Bar. Follow the instructions on this page to create the button, and use this code in the shortcut:
historypath = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
lastexpression = historypath(end);
eval(lastexpression)
Alternatively, if you would like to rerun the last batch of commands (if you enter multiple lines, but run them all at once, they're a batch), you might have to get a bit trickier. You could create a button shortcut that uses xmlread to open the history file. The history file for me is located at:
C:\Users\<username>\AppData\Roaming\MathWorks\MATLAB\R2015a\History.xml
The folder should be the same as your prefdir. You could then parse out all the commands at the end that have the same batch number, and run them with eval.
I hope this helps!
-Cam
  4 个评论
Simone Gaiarin
Simone Gaiarin 2015-9-8
This is actually a good-enough solution. Thanks
Cam Salzberger
Cam Salzberger 2015-10-9
I just found out that you can apparently use Ctrl+0 (zero, not "O") to focus on the command window. This will grab the focus from anywhere else in the main MATLAB GUI, or the editor, though not from an undocked figure window. But it does mean that sometimes you could do Ctrl+0 -> up arrow -> Enter to rerun the last command.

请先登录,再进行评论。


Guillaume
Guillaume 2015-9-8
An option is to go outside of matlab and use a macro recording program to would reproduce all the steps you've mentioned when you type a key combination of your choice.
There's probably one out there clever enough to be able to figure out the position of matlab's window wherever it is, so it is always going to click inside it.

Conglian Pan
Conglian Pan 2018-1-5
on my MacBook, I simply use the up arrow key and a list of history command would appear. The bottom one is the last command, and you just hit enter to rerun it. If you want to go few commands back, it is also very simply, just keep using up arrow key to select and then hit enter key.
  2 个评论
Walter Roberson
Walter Roberson 2018-1-5
Right, but this requires focusing on the command window in order to click there. Cam was hoping for something that did not require doing that focusing; for the moment Cam has settled for a key sequence that does the focusing, which avoids having to mouse to an appropriate area and click there. (As someone who sometimes has spells of repetitive strain injury, I can appreciate why one might prefer to avoid using the mouse!)
Alireza yarmohammadi
hy i think ctrl+TAB would be a good one!!!!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by