Displaying figures/message boxes in a while loop
4 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (2 件)
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
2015 年 11 月 1 日
This is a comparison, not an assignment:
MessageShownAlready == false
Perhaps you mean:
MessageShownAlready = false
with one equal character.
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
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 件のコメント
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 Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!