While Loop Inside a function
5 views (last 30 days)
Show older comments
How do I write a while loop inside a function?
I want a function that will continue to run until a user inputs a number between the given choices.
0 Comments
Answers (2)
Star Strider
on 7 Nov 2019
Write it the same way you would write a while loop in a script.
Also consider putting in a counter with a limited number of iterations, or some other condition (such as testing for an empty response), so that it does not become an infinite loop.
0 Comments
Walter Roberson
on 7 Nov 2019
function user_choice = ask_connectivity
user_choice = [];
while ~isscalar(user_choice) || ~ismember(user_choice, [4, 8, 26])
user_choice = input('Enter connectivity to use, 4, 8, or 26? ');
end
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!