string = '';
last_was_bs = false;
while true
char = GetChar;
switch (abs(char))
case {13, 3, 10}
break;
case 8
if ~last_was_bs
if ~isempty(string)
string = string(1:length(string)-1);
end
end
last_was_bs = true;
otherwise
string = [string, char ' '];
last_was_bs = false;
end
However, I don't think you are going to find this very suitable. The extra space that is inserted after every character increases the length of the string, and would thus be what was trimmed out by the first backspace. In order to be able to actually remove the last character typed, the user needs to be able to type at least two backspaces. Unless, that is, the extra space was not added.
It is poor programming practice to name a variable "char", as that interferes with using the MATLAB char() function.
Best Answer