MATLAB - identifying each character in a set

I have the following code in MATLAB where the user has to enter a word "precipitations"

by letter. After entering one letter, the user must press Enter , and the program checks the correctness of the entered letter.

Now I would like to change the program so that the user does not need to press Enter after entering a letter. Is there any operator or function in MATLAB that reacts to every button that is pressed, so there is no need to press Enter ?

disp('Please enter "precipitations" without errors')
target=('precipitations');
n=size(target); n=n(2); % Characters number

for i=1:n;
    YourInput(i)=input('','s');
    if YourInput(i)==target(i)
        disp('OK. Please, input the next symbol')
        i=i+1;
    else
        disp('Error. Please try again.')
        break
    end
end

      

+3


source to share


1 answer


As far as I know, there is no built-in MATLAB function for this. However, there is a feature getkey

on MATLAB File Exchange .

You can download this function and change your code to use

YourInput(i) = getkey();

      

-



I was of course wondering how this can be achieved and it does the following: they create a new shape with the window size 0,0

in position (1,1)

. You will see a new shape in the lower left corner of the screen.

Then the inverse function KeypressFcn

is executed, which is executed whenever a key is pressed. The pressed key is saved in the field UserData

in the figure and returned as a variable. The interesting parts of the function (and minimal example) are:

fh = figure(...
    'keypressfcn','set(gcbf,''Userdata'',double(get(gcbf,''Currentcharacter''))) ; uiresume ', ...
    'position',[0 0  1 1] ...
);
uiwait ;
key = get(fh,'Userdata') ;
delete(fh) ;

      

+2


source







All Articles