MATLAB: 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

dtmf

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

Best Answer

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.