I would like to remove whatever I just typed - and the last shown command - from the matlab console display. Needless to say, this would be ideal for pranksters (but this is of course strictly for academic purposes only). This is as far as I have gotten (based on this related answer):
hist = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory; %get history last = strjoin(cell(hist(end-2:end)),' '); %convert history to string fprintf(repmat('\b',1,numel(last))); %replace characters of string with whitespace
However I can only access the last typed command (through the command history) - not the last displayed command (which would be ideal). Any ideas how to solve this?
1 Answers
Answers 1
Disclaimer: I don't advise doing this.
The MATLAB CommandWindow contents are stored as a CmdWinDocument
, which is an extension of the Java PlainDocument
type, and an interface of the Document
type. The current window can be accessed using the command:
com.mathworks.mde.cmdwin.CmdWinDocument.getInstance
In theory, you should be able to remove text from the command window using something like:
doc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance endpos = doc.getEndPosition doc.remove(endpos-10,10)
which would, in theory, remove the last 10 character from the document. You may have to call the removeUpdate
function as well. Obviously problems will be caused by the fact that these commands will be appended to the document during this process. I have not tested this, and you're likely to cause problems with internally stored offsets within the CmdWinDocument
class, so use at your own risk.
0 comments:
Post a Comment