Why isn't this breaking the loop?

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

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

So sorry but I did call it but missed that part when pasting my code, I've edited the post to include it now
continueChoice is not initialized anywhere in your posted code.
Your cont function is declared to return the variable in its workspace that is named continueChoice.
function continueChoice = cont()
However, when you call cont, you call it with no output arguments.
cont()
This does not "automagically" create or populate the variable named continueChoice in the workspace in which cont was called. [That's a not uncommon misconception that users have about how MATLAB works.] Like Las Vegas, generally what happens inside a function stays inside that function (unless you specifically and explicitly return it from that function. Yes, I'm simplifying things a bit.)
If you want to return the variable named continueChoice from cont you need to both define cont to return it and call cont with an output argument, by which name the data from the continueChoice variable inside cont will be known in its caller's workspace.
"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.
That worked, tysm peeps!

Sign in to comment.

More Answers (0)

Products

Release

R2025b

Asked:

on 25 Nov 2025

Commented:

on 25 Nov 2025

Community Treasure Hunt

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

Start Hunting!