While function must have correct input in order to continue

1 view (last 30 days)
Hello everyone
I have a function that i need the user to enter the right statistic into. It's a part of a Main script where you first choose which function to use.
function data = calculate(data,option)
while true
if strmcp(option, '......')
condition
elseif strmcp(option, '......')
condition
else
** ENTER INPUT AGAIN AND EXECUTE CONDITION **
end
end
So i need it to go back to the while loop, and look for a correct input in the function, and then calculate the option choosen.
I've tried using ...... = input('State correct input: ') but it doesn't work.
I hope some of you can help me.
Thanks in advance! :)

Accepted Answer

Adam
Adam on 8 Apr 2015
Why don't you just enforce a valid input to the function in the first place? Is it a requirement to allow the function to be called with arbitrary nonsense as many times as the user likes until they enter a supported option?
I would just use
option = validatestring( option, { 'option1', 'option2' } );
at the start of the function, obviously replacing 'option1' and 'option2' with whatever strings you support.
A function really shouldn't have to go prompting a user to keep entering a new input if they input rubbish the first time. User prompting should be done outside the function, catching the error message in a try-catch statement if you want to support nonsense inputs (or doing the validation in the script itself).
  3 Comments
Adam
Adam on 8 Apr 2015
The line I put in my answer will throw an error if the variable 'option' does not match one of the strings in the list (in this case 'option1' or 'option2').
Whether you wish to allow the function to be called and then handle the resulting error (outside the function) or do the validation before you call the function (and do a loop for user input until that validation is satisfied) is mostly up to you, but either way if a user can just type in any old raw string you will need to do validation of that type and catch the error.
e.g.
option = '';
while true
try
option = validatestring( getOptionFromUser( ), { 'option1', 'option2' } );
catch
continue;
end
break
end
data = calculate( data, option );
That would be one way of doing it (I haven't tested that code so there could be a syntax error).
Personally I would probably just define a default option and if the user enters an invalid option I would just use the default. But I always have a GUI front end for my work so I do this kind of validation at the UI end by e.g. using a popup list where the user cannot select an invalid option.
Kasper Jørgensen
Kasper Jørgensen on 8 Apr 2015
Personally i would also go a popup window, in order to choose between the different calculations for this function. The professor i have, just don't agree with that idea unfortunately.
My problem was i didn't understand how to use the validation, which i do now and works as intended.
Thank you so much for your time and help!

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!