I found the answer to the question. This seems to be a bug in matlab. See https://www.mathworks.com/matlabcentral/answers/494574-pause-clc-fprintf-pause-fprintf-is-ignored for more informatin.
input() only prints its prompt on first iteration of loop
7 ビュー (過去 30 日間)
古いコメントを表示
I'm attempting to create a program that gives the output of a chosen function when the user inputs the function's number. The program will continue forever prompting and giving answers. The first time the loop runs, I enter in '1', and everything works as I expect. The second and subsequent times, input() does not output its prompt. Input() still runs and will accept input but doesn't display anything. What did I do wrong here?
close all
clear
%% Problem Selection Flow Control
while true
clc
p_num = input("Please enter a problem number. Type 'exit' to exit program.","s");% Promt user for problem number
if(p_num == "exit")% Check if user wants to exit program
clc
break
end
% Tests to see if user input invalid data. If so, notify user and
% restart loop.
try
eval(strcat("result = problem",p_num,';'));% Evaluates the function associated with the selected problem
catch
fprintf("\nThe entered problem doesn't exist. Press any key to try again.");
pause
continue
end
clc
fprintf(strcat("The answer for problem ",p_num," is %f"),result(1));
fprintf(" and %f",result(2:end));% Output the result from the selected problem
fprintf("\nPress any key to continue.");
pause% Waits until user presses any key
end
%% Problem 1
function result = problem1()
A1 = atan(2);
A2 = atan(100);
result = [A1 A2];
end
5 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!