Why isn't this breaking the loop?

37 views (last 30 days)
Ayaan
Ayaan on 25 Nov 2025 at 18:59
Commented: Ayaan on 25 Nov 2025 at 23:01
I have tried to use an ouptut argument to make a variable and use that variable in my main script, however when it is returning the value I want, it still isnt breaking the while loop?
Code for the function:
function continueChoice = cont()
continueChoice = listdlg("SelectionMode", "single", "ListString", ["Continue", "Exit"], "PromptString", "Would you like another statistic?")
end
Code in the main script:
data = readtable('alltimeteams.xlsx','VariableNamingRule','preserve');
x = 0
while le(x,210)
x = x+1
choice=listdlg('SelectionMode','single', 'ListString',data.Franchise,'PromptString','Please choose a franchise'); % all possible franchises
if isempty(choice)
fprintf("Please choose a franchise next time \n")
break; % Exit the loop if no franchise is chosen
end
% the following is the problematic part
Wpercentchoice = listdlg("SelectionMode","single","PromptString","Would you like the win%?","ListString",["Yes","No"]);
if Wpercentchoice == 1
WinPercentcalc(data,choice); % calculates the overall win percentage (seperate function)
cont()
end
if isempty(continueChoice)
fprintf("Thank you!") % exit the loop if another statistic is not wanted
break;
elseif continueChoice == 2
fprintf("Thank you!") % exit the loop if another statistic is not wanted
break;
end
This is what shows in the command window after selecting "exit" in the listdlg
continueChoice =
2
ans =
2
continueChoice is 2, so shouldnt it work? It just carries on with the rest of my code instead.
Please lmk if any more info is needed.
P.S the reason I've made a function which is so short is because one of the criteria for the project im doing is that it has to have a function which has an ouptut argument and this is the only thing I could make one for , I understand it'd be a lot easier just doing it in the main script.

Accepted Answer

dpb
dpb on 25 Nov 2025 at 19:46
if isempty(continueChoice)
...
but
function continueChoice = cont()
continueChoice = listdlg("SelectionMode", "single", "ListString", ["Continue", "Exit"], "PromptString", "Would you like another statistic?")
end
is your function definition, so you need to call cont(). Referencing the name of the return variable in the function definition doesn't count to call the function; the calling routine knows nothing about what the local variables are named; the return value of the function is known by that variable name inside the function, yes, but it must be assigned to some variable in the caller or the function name used to get the first defined argument of the function without the explicit assignment.
continueChoice is the variable which was previously set to 2 and is never redefined nor cleared so it's going to remain in your workspace.
It would be best to set the choice to [] every pass before the selection is called
  6 Comments
dpb
dpb on 25 Nov 2025 at 22:11
"So sorry but I did call it but missed that part..."
if Wpercentchoice == 1
WinPercentcalc(data,choice); % calculates the overall win percentage (seperate function)
cont()
end
But you didn't return the value to a local variable so it simply will echo the result to the default 'ans' variable but there's no way to use its result in the calling routine.
Again, the calling routine knows nothing about what the variables are in the scope of the function.
You need
if Wpercentchoice == 1
WinPercentcalc(data,choice); % calculates the overall win percentage (seperate function)
continueChoice=cont();
end
Not having that or any other way to set a value for continueChoice in the calling routine is what @Walter Roberson pointed out as well.
Ayaan
Ayaan on 25 Nov 2025 at 23:01
That worked, tysm peeps!

Sign in to comment.

More Answers (0)

Products


Release

R2025b

Community Treasure Hunt

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

Start Hunting!