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

 Respuesta aceptada

Jan
Jan el 11 de Mayo de 2018
Editada: Jan el 11 de Mayo de 2018
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 comentarios

Aash
Aash el 11 de Mayo de 2018
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 el 11 de Mayo de 2018
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 el 11 de Mayo de 2018
Editada: Jan el 12 de Mayo de 2018
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 el 12 de Mayo de 2018
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 el 12 de Mayo de 2018
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 el 12 de Mayo de 2018
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 el 12 de Mayo de 2018
Thank you so much for the help Jan.
Jan
Jan el 12 de Mayo de 2018
You are welcome, Aash.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Mayo de 2018

Comentada:

Jan
el 12 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by