Displaying figures/message boxes in a while loop

조회 수: 4 (최근 30일)
Ryan
Ryan 2015년 10월 27일
댓글: Image Analyst 2015년 11월 3일
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

답변 (2개)

Jan
Jan 2015년 10월 27일
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
Jan
Jan 2015년 11월 1일
This is a comparison, not an assignment:
MessageShownAlready == false
Perhaps you mean:
MessageShownAlready = false
with one equal character.
Image Analyst
Image Analyst 2015년 11월 1일
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().

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


Image Analyst
Image Analyst 2015년 10월 31일
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
Ryan
Ryan 2015년 11월 3일
Thanks Image Analyst, that works perfect for what I'm trying to do!
Image Analyst
Image Analyst 2015년 11월 3일
You're welcome. If it's the best answer, you can click the link to "Accept this answer". Thanks in advance.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by