wait bar in MATLAB gui in nested FOR loop

I have to show a progress bar in matlab guide that appears while running FOR loop and shows progress for each iteration. Moreover, the user should not be able to close it until the calculation stops, so as to prevent the user from making any changes. Example code is as follows
MBTs = minMBT:stepMBT:maxMBT;
MATs = minMAT:stepMAT:maxMAT;
i=1;
for MAT = MATs
j = 1;
for MBT = MBTs
%some calculations
j = j + 1;
end
i = i + 1;
end

 채택된 답변

Jan
Jan 2018년 5월 11일
편집: Jan 2018년 5월 11일

0 개 추천

MBTs = minMBT:stepMBT:maxMBT;
MATs = minMAT:stepMAT:maxMAT;
n = length(MATs) * length(MBTs);
c = 0;
H = waitbar(0, 'Please wait...');
for i1 = 1:length(MATs)
MAT = MATs(i1);
for i2 = 1:length(MBTs)
MBT = MBTs(i2);
%some calculations
% Update the waitar:
if ~ishghandle(H)
error('User closed waitbar.');
end
c = c + 1;
waitbar(c / n, H, sprintf('%d of %d', c, n));
end
end
It would be brute to forbid, that the user closes the waitbar. Imagine that the loop runs for 2 hours for unknown reasons. Do you really block a premature stopping? User hate such restrictions for good reasons.

댓글 수: 9

Aash
Aash 2018년 5월 11일
thanks Jan for such a detailed answer. I actually want to disable the cancel function so that no changes can be made by the user during the execution otherwise it will become mess
Aash
Aash 2018년 5월 11일
There is one more thing that I want to change the arrow cursor to hour watch (loading cursor) and it should change back to the arrow curser after loof stops.
Jan
Jan 2018년 5월 11일
편집: Jan 2018년 5월 12일
What does "disable the cancel function" mean? Which cancel function? If you do not specify the 'CreateCancelBtn' property of waitbar, there is no Cancel button.
Under Windows you could use FEX: WindowAPI to hide the X button to close the window:
H = waitbar(0, 'Please wait...');
WindowAPI(H, 'Button', 'off');
I've showed in my code how to catch a closed waitbar window. You should be able to start a proper cleanup instead of the error.
To change the cursor:
H = waitbar(0, 'Please wait...');
set(H, 'Pointer', 'watch');
...
set(H, 'Pointer', 'arrow');
Aash
Aash 2018년 5월 12일
I have tried doing this for cursor but it only changes cursor shape on the window where the waitbar is showing, What if I want to chnage it on entire GUI?
Jan
Jan 2018년 5월 12일
What is "the entire GUI"? Matlab's command window or the operating system? You can set the cursor in each window by using its handle. So if you have e.g. a figure created by GUIDE, use its handle for the above commands.
Aash
Aash 2018년 5월 12일
I mean Matlabs uipanel where the buttons are present. I am attaching the figure the cursor is curently chnaging when i put it on waitbar not the window below it
So you mean the window called "Untitled"? It seems to be created by GUIDE. And I guess, that the waitbar is opened inside one of the callbacks? Then insert there:
FigH = ancestor(hObject, 'figure');
set(FigH, 'Pointer', 'watch');
...
set(FigH, 'Pointer', 'arrow');
You have the figure handle inside the struct handles also, but I'm not sure, how it is called. You can check this by your own: Set a breakpoint in the code and display the contents of the handles struct.
Aash
Aash 2018년 5월 12일
Thank you so much for the help Jan.
Jan
Jan 2018년 5월 12일
You are welcome, Aash.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

태그

질문:

2018년 5월 11일

댓글:

Jan
2018년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by