I know this thread is ancient but i was looking for a solution to this. A much safer way of obtaining the last typed command is:
hist = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
last = string(hist(end));
hist = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
last = string(hist(end));