Help with writing a function to check a users input
Mostrar comentarios más antiguos
I am writing a script add reverb to an input file and have it so that the user can input the reverb time and amount of the direct sound they want mixed in. I've been trying to write a function to check the user input but have been having errors with the console getting stuck in the else statement no matter what input I have. This is the code for the function:
function ds = InputCheck(prompt1, prompt2)
instr = input(prompt1); % asks user for an input
instr = str2double(instr);
while isnumeric(instr) == 1
if (instr > 0) && (instr < 1) % limits for the input value
ds = instr; % outputs the value
break
else
instr = input(prompt2);
instr = str2double(instr);
end
end
And I have been calling it with this:
ds = InputCheck('Enter how much direct sound to mix in: ','Enter a number between 0 and 1: ');
This is what I get from the console:
>> ProcessingCombScript
Enter how much direct sound to mix in: 0.1
Enter a number between 0 and 1: 0.2
Enter a number between 0 and 1: 0.3
Enter a number between 0 and 1: 0.4
Enter a number between 0 and 1: 1
Enter a number between 0 and 1: 1.5
Enter a number between 0 and 1: -1
As you can see no matter what I enter I get the second prompt message. I know I've probably made a mess of this but any help and improvements would be greatly appreciated.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!