Displaying figures/message boxes in a while loop

14 views (last 30 days)
I'm having trouble displaying information to the user of my program. I want to have a figure or messagebox that comes up once when two different possibilities become true. The problem is that these can become true or false at any given time under certain circumstances, and the calculations are taking place inside a while loop. Also, while the two conditions are true - with the way I'm doing it now, the figure message box pops up during each iteration. I only want this to come up once. Any suggestions or advice would be greatly appreciated. Here is an example of my code.
while (expression)
%CALCULATIONS.......
if AceHearts && KingHearts == true
msgbox('Your odds of winning are 67%')
elseif AceHearts && KingClubs == true
msgbox('Your odds of winning are 65%')
end

Answers (2)

Jan
Jan on 27 Oct 2015
What about using a flag:
messageShownAlready = false;
while (expression)
%CALCULATIONS.......
if AceHearts && KingHearts == true
if ~messageShownAlready
msgbox('Your odds of winning are 67%')
messageShownAlready = true;
end
elseif AceHearts && KingClubs == true
if ~messageShownAlready
msgbox('Your odds of winning are 65%')
messageShownAlready = true;
end
end
end
  3 Comments
Jan
Jan on 1 Nov 2015
This is a comparison, not an assignment:
MessageShownAlready == false
Perhaps you mean:
MessageShownAlready = false
with one equal character.
Image Analyst
Image Analyst on 1 Nov 2015
Please explain why you aren't using uiwait() like I suggested in my answer. That will still let each message box come up, like you want, but it will prevent hundreds of them coming up all at once: "the problem I was having with the messagebox coming up hundreds of times." msgbox() does not wait for you to click OK while wrapping it in uiwait() makes it wait for you.
There is a 'modal' input argument option but it doesn't seem to work according to the definition the rest of the world uses (i.e. it doesn't cause it to wait). If you just want all the messages to go to the command window instead of piling up hundreds of message boxes on the screen (which must all be clicked to clear them), you can use fprintf() instead of msgbox().

Sign in to comment.


Image Analyst
Image Analyst on 31 Oct 2015
Wrap msgbox() or helpdlg() in a uiwait() so it will wait for the user to click OK on it.
uiwait(helpdlg('blah blah blah'));
or better yet, use questdlg().
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(buttonText, 'Cancel')
return;
end
so the user can bail out if they want.
  2 Comments
Ryan
Ryan on 3 Nov 2015
Thanks Image Analyst, that works perfect for what I'm trying to do!
Image Analyst
Image Analyst on 3 Nov 2015
You're welcome. If it's the best answer, you can click the link to "Accept this answer". Thanks in advance.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!