Limiting number of characters to three positions (ranging from 0–100)

3 vues (au cours des 30 derniers jours)
Lisa M
Lisa M le 9 Fév 2020
Réponse apportée : Lisa M le 9 Fév 2020
So far the code restricts character responses to keys 0-9. However you could type 300. I would like to limit the response in a way that only 0–100 responses are possible. Any suggestions? Thanks in advance.
string = '';
while true
% assemble the cue/target display
output = [msg, ' ', string];
% show it
Screen('CopyWindow',origwindow, windowPtr);
Screen('DrawText', windowPtr, output, x, y, textColor, bgColor);
Screen('Flip', windowPtr,0);
char = GetChar;
switch (abs(char))
case {13, 3, 10}
% ctrl-C, enter, or return
return;
case 8
% backspace
if ~isempty(string)
string = string(1:length(string)-1);
end
% 0-9
case {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58}
string = [string, char];
end
end
  4 commentaires
Walter Roberson
Walter Roberson le 9 Fév 2020
Change my <= 2 into <= 1
Lisa M
Lisa M le 9 Fév 2020
Thanks!! It works.

Connectez-vous pour commenter.

Réponse acceptée

Lisa M
Lisa M le 9 Fév 2020

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by