User input to repeat a script

trying to give users the ability to re-run a program with the following
while(1)
%%code
m = input('Enter Y to start over. Enter Quiz to move on to the Quiz: ','s');
if m==['Quiz']
break
end
end
next section of code is the quiz
This loop for some reason makes certain graphs not get displayed, and for the program to not run right. the code in the middle is like 200 lines, and runs perfect without the whileloop.
any suggestions on how i could alternatively accomplish this same task of giving the user the ability to start the script from the start again?

답변 (3개)

Walter Roberson
Walter Roberson 2023년 10월 11일

0 개 추천

if strcmp(m, 'Quiz')
Remember that in MATLAB, characters with ' around them forms a character vector. 'Quiz' is, for many purposes in MATLAB, the same as uint16([81 117 105 122])

댓글 수: 6

Matthew
Matthew 2023년 10월 11일
The while loop it's self works, it just messes with the code. As soon as i add the while loop certain graphs wont be displayed from user input, and the whole thing just doesn't run correctly. i know it's not the code itself becuause it works perfectly fine without being sandwhiched in the whileloop
Walter Roberson
Walter Roberson 2023년 10월 11일
Did you remember to use hold off for the axes that you used hold on for ?
Matthew
Matthew 2023년 10월 11일
i have no hold on or hold off, pehaps i need to use them to make the while loop work correctly?
Matthew
Matthew 2023년 10월 11일
The data be plotted is just one single output, on it's own individual graph
Dyuman Joshi
Dyuman Joshi 2023년 10월 11일
It would be better if you could share the relevant code.
Matthew
Matthew 2023년 10월 11일
lets use this code as an example
while(1)
z = linspace(0,4*pi,100);
r = 2*sin(z);
plot(z,r)
xlabel('z')
ylabel('2sin(r)')
title('Plot of the Sine Function')
x = linspace(0,2*pi,100);
y = sin(x);
plot(x,y)
xlabel('x')
ylabel('sin(x)')
title('Plot of the Sine Function')
m = input('Enter Y to start over. Enter Quiz to move on to the Quiz: ','s');
if m==['Quiz']
break
end
end
only the first graph is being displayed, i want both to be displayed, with the ability to restart the program

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2023년 10월 11일

0 개 추천

When you call "high level plotting commands" such as plot() or contour() or imshow() then by default they clear the current axes before drawing. You can use "hold on" to prevent that

댓글 수: 1

Matthew
Matthew 2023년 10월 11일
편집: Matthew 2023년 10월 11일
i figured it out. i added
figure after every plot
then close all if the loop was selected

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2023년 10월 11일
편집: Image Analyst 2023년 10월 11일

0 개 추천

while (1)
% Put script code here.
% Now ask use if they want to do it again.
promptMessage = sprintf('Do you want to run it again,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return; % or break.
end
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2023년 10월 11일

편집:

2023년 10월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by