How do I programmatically access the last expression typed in the MATLAB Command Window in MATLAB 7.7 (R2008b)?

I want to get the last expression typed into the Command Window as a string saved to a variable.

 Accepted Answer

The last Command Line expression can be obtained by accessing the saved history of expressions typed into the Command Window, and indexing the last expression as shown below:
historypath = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
lastexpression = historypath(end)
If the above is executed in the Command Window itself, then remember to index into location (end-2) instead of (end), as shown below:
historypath = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
lastexpression = historypath(end-2)

More Answers (0)

Categories

Products

Tags

Community Treasure Hunt

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

Start Hunting!