Hi Everyone! I'm trying to have a for loop that will repeat the question when the input value is greater than or less than to required value
Afficher commentaires plus anciens
here's what i have:
Fs = 8000;
t =0.5;
n=[1:ceil(t*Fs)];
T1 = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1209/Fs)*n); %T1=key one(1)
T2 = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1336/Fs)*n); %T2=key two(2)
T3 = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1477/Fs)*n); %T3=key three(3)
T4 = sin(2*pi*(770/Fs)*n) + sin(2*pi*(1209/Fs)*n); %T4=key four(4)
T5 = sin(2*pi*(770/Fs)*n) + sin(2*pi*(1336/Fs)*n); %T5=key five(5)
T6 = sin(2*pi*(770/Fs)*n) + sin(2*pi*(1477/Fs)*n); %T6=key six(6)
T7 = sin(2*pi*(852/Fs)*n) + sin(2*pi*(1209/Fs)*n); %T7=key seven(7)
T8 = sin(2*pi*(852/Fs)*n) + sin(2*pi*(1336/Fs)*n); %T8=key eight(8)
T9 = sin(2*pi*(852/Fs)*n) + sin(2*pi*(1477/Fs)*n); %T9=key nine(9)
T0 = sin(2*pi*(941/Fs)*n) + sin(2*pi*(1336/Fs)*n); %T0=key ten(10)
T10 = sin(2*pi*(941/Fs)*n) + sin(2*pi*(1209/Fs)*n); %T10=key asterisk(*)
T11 = sin(2*pi*(941/Fs)*n) + sin(2*pi*(1477/Fs)*n); %T11=key number sign(#)
v=input('NUMBER OF KEYS TO ENTER: ');
w=input('ENTER KEY/S: ' ,'s');
x=str2num(w); %#ok<ST2NM>
y=length(x);
while(1)
if (v>y)&&(v<y)
disp('ERROR')
w=input('ENTER KEY/S: ' ,'s');
elseif v==1;v=y; %when the number of key/s entered is/are one(1)
a=x(1,1);
if a==0;subplot(4,3,11);xlabel('KEY0');plot(T0); sound(T0);
elseif a==1;subplot(4,3,1);xlabel('KEY1'); plot(T1); sound(T1)
elseif a==2; subplot(4,3,2);xlabel('KEY2');plot(T2);sound(T2);
elseif a==3;subplot(4,3,3);xlabel('KEY3');plot(T3); sound(T3);
elseif a==4;subplot(4,3,4);xlabel('KEY4');plot(T4); sound(T4);
elseif a==5;subplot(4,3,5);xlabel('KEY5');plot(T5); sound(T5);
elseif a==6;subplot(4,3,6);xlabel('KEY6');plot(T6); sound(T6);
elseif a==7;subplot(4,3,7);xlabel('KEY7');plot(T7); sound(T7);
elseif a==8;subplot(4,3,8);xlabel('KEY8');plot(T8); sound(T8);
elseif a==9;subplot(4,3,9);xlabel('KEY9');plot(T9); sound(T9);
elseif a==10;subplot(4,3,10);xlabel('KEY*');plot(T10);sound(T10);
elseif a==11;subplot(4,3,12);xlabel('KEY#');plot(T11);sound(T11);
end
end
end
5 commentaires
Guillaume
le 16 Sep 2015
I don't understand why you first ask the user to enter the number of keys he's going to be entering. Then ask him to enter the keys and check that he's entered the same number of keys. Why ask in the first place, just use the number of keys entered.
It's difficult to understand what you're trying to do with your code. Your comments imply that keys (whatever you mean by that word) can either be single digit (0 to 9) double digit (10, so you don't mean keyboard keys), or symbols (* and #). But then you convert the key to a number with str2num which is obviously not going to work with symbols.
You also have an unnending while loop whose purpose is unknown, a very strange comparison (v>y)&&(v<y) which is never going to be true, and code that is only executed if the user has said he will only enter one key (so again, why bother asking?), followed by an assignment to v that does not seem to serve any purpose.
And finally, I have to ask, at what point do you decide that writing the same line of code with only a small variation is a good hint that a better implementation is needed?
chrys
le 17 Sep 2015
chrys
le 17 Sep 2015
The OP's requirements are actually very straightforward, but you might get a better idea if you read their earlier question here:
Also note that they accepted my answer which gave them much simpler and yet more versatile code (e.g. no conversion to numeric, logical indexing, etc) than what they give here... why did they decide to go back to their unwieldy and buggy code?
Guillaume
le 17 Sep 2015
Well, we've come back with pretty much the same answer (I wasn't aware of the previous question).
It looks like the OP wanted to add a retry (but I see you already answered that as well) and wanted to ask the number of keys that were going to be entered, which is a bad idea and unnecessary.
Réponse acceptée
Plus de réponses (1)
Thorsten
le 16 Sep 2015
This results in a string w of v characters:
v=input('NUMBER OF KEYS TO ENTER: ');
w=input('ENTER KEY/S: ' ,'s');
while length(w) ~= v
w=input('ENTER KEY/S: ' ,'s');
end
Catégories
En savoir plus sur MATLAB Mobile dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!