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

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

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?
i really appreciate your help. and i tried the codes you gave. but theirs something wrong in assertion, can you help me out. i really need your help. beginners problem. i not so good on this
assert(size(times, 1) == numtimes); there some thing wrong in that code pls help me figure out.
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?
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.

Connectez-vous pour commenter.

 Réponse acceptée

As per my comment to your question, it's not clear what you're trying to achieve with your code. In any case, when you're writing eleven times the same line, it's time to rethink your code:
Fs = 8000;
t =0.5;
n=[1:ceil(t*Fs)];
times = [697 1209
697 1336
697 1477
770 1209
770 1336
770 1477
852 1209
852 1336
852 1477
941 1336
941 1209
941 1477];
numtimes = 12; assert(size(times, 1) == numtimes)
for tidx = 1:numtimes
T{tidx} = sin(2*pi*(times(tidx, 1)/Fs)*n) + sin(2*pi*(times(tidx, 2)/Fs)*n); %formula entered only once!
end
while true %possibly this is what you were trying to do
%note that a key is a number from 1 to numtimes
%multiple numbers can be entered if they're separated by spaces, comma, or semicolon.
keys = input('enter keys (numbers separated by spaces: ', 's');
keys = str2num(keys);
%check that 'keys' is between 1 and the number of rows in times, and check that all are integers
if ~isempty(keys) && max(keys) <= numtimes && min(keys) >= 0 && all(mod(keys, 1) == 0)
break; %if the check is passed, break out of while loop
end
fprintf('Some entered data is not valid\n');
end %loop around if the check wasn't true, i.e reprompt the user
keylabels = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '#'};
for key = keys(:)'
subplot(4,3, key);
xlabel(sprintf('KEY%s', keylabels{key}));
plot(T{key});
sound(T{key});
end
Notice that not a single line of code is repeated.

4 commentaires

I've fixed the code. The purpose of the assert was to make sure that the numtime variable was equal to the number of rows in times.
Since apparently I can't even count up to twelve it's a good thing I put it there as otherwise the code would have worked most of the time and failed only when '12' would have been entered.
The purpose of assert is to check that a condition you expect is true and stop the code otherwise.
error in this part if max(keys) <= numtimes && min(keys) >= 0 && all(mod(keys, 1) == 0) %if the check is passed, break out of while loop end
A much simpler solution is to use the code that I gave to an earlier question by the same OP. It uses basic numeric and char arrays (no cells), uses only vectorized numeric operations, and just one simple loop for the plots.
That would have happened if you entered some invalid values. Anyway, I've fixed it now, along with another bug and I've made more explicit what is expected to be entered.
The code expects a list of numbers from 1 to 12 separated by spaces. All the input below are valid:
1
8 12 4 6
1 1 1 1 2 2 2 2 %will cause the plots to be redrawn several times and sound to repeat.
1 2 3 4 5 6 7 8 9 10 11 12

Connectez-vous pour commenter.

Plus de réponses (1)

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by