MATLAB: Rerun code using if statement simple how can I rerun a code once it's finished? in order to type in new values. I'd like an if statement at the end of my code asking the user whether they want the code to rerun and then have it restart. Thanks Best Answer Try this:numIterations = 1;maxIterations = 20;while numIterations < maxIterations % Run your code now.... % Now ask user if they want to run it again. promptMessage = sprintf('Do you want to process again,\nor Quit processing?'); titleBarCaption = 'Continue?'; buttonText = questdlg(promptMessage, titleBarCaption, 'Process Again', 'Quit', 'Process Again'); if contains(buttonText, 'Quit') break; % Break out of the while loop. end numIterations = numIterations + 1;end Related SolutionsMATLAB: Ask User to Continue Set the infinite loop. I did not check if this code runs, but the idea is this:while(1)%%your code herem=input('Do you want to continue, Y/N [Y]:','s')if m=='N'breakendendAlso, to avoid check up for extra cases from user input I would use function 'menu' instead of 'input':while(1)%%your code herechoice = menu('Press yes no','Yes','No');if choice==2 | choice==0 break;endend MATLAB: How to Enter Threshold Value Depend on the Selected Image Try this to ask your user for an integer value for the threshold:% Ask user for one integer number.defaultValue = 45;titleBar = 'Enter an integer value';userPrompt = 'Enter the threshold';caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.% Round to nearest integer in case they entered a floating point number.integerValue = round(str2double(cell2mat(caUserInput)));% Check for a valid integer.if isnan(integerValue) % They didn't enter a number. % They clicked Cancel, or entered a character, symbols, or something else not allowed. integerValue = defaultValue; message = sprintf('I said it had to be an integer.\nTry replacing the user.\nI will use %d and continue.', integerValue); uiwait(warndlg(message));end Related QuestionRepeating or rerunning a loop
Best Answer