Help with writing a function to check a users input
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jakub Thurlow
le 30 Nov 2018
Commenté : Jakub Thurlow
le 30 Nov 2018
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.
0 commentaires
Réponse acceptée
Mark Sherstan
le 30 Nov 2018
You were on the right track. To make it easier on yourself you can include a string inside the input function like I did below. Also you want to use return instead of break once your solution is satisfied. Let me know if you need any clarification.
function ds = InputCheck()
x = input('Enter how much direct sound to mix in: ');
while 1
if (x > 0) && (x < 1) % limits for the input value
ds = x; % outputs the value
return
else
x = input('Enter a number between 0 and 1: ');
end
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!