Help with writing a function to check a users input
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jakub Thurlow
am 30 Nov. 2018
Kommentiert: Jakub Thurlow
am 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 Kommentare
Akzeptierte Antwort
Mark Sherstan
am 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
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!