While Loop Inside a function

조회 수: 5 (최근 30일)
Christina Rogers
Christina Rogers 2019년 11월 7일
답변: Walter Roberson 2019년 11월 7일
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.

답변 (2개)

Star Strider
Star Strider 2019년 11월 7일
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.

Walter Roberson
Walter Roberson 2019년 11월 7일
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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by