Add a waitbar to a function execution

Hi,
I wrote a function that uses a 3D stack of images as an input, and processes them. As a result of it, a new 3D matrix is created.
I am strugling to add a proper waitbar to this, since it can take from a few seconds to several minutes to complete this task inside this function (depending on the size of the input 3D matrix of images).
I tried several different solutions, but did not succeed.
My input is a stack of 3D images called QQ(MxNxP)
h = waitbar(0,'Please wait...');
steps=size(QQ, 3); %%to extract the number of images inside 3D matrix
for step = 1:steps
A=myFunction(QQ) %%my function goes here, not sure if I need to add something like A(:,:,step)=myFunction(QQ(:,:,step))
waitbar(step / steps)
end
close(h)
How can I properly define the waitbar in order to track execution time of my function?
Thanks!

3 comentarios

Adam
Adam el 24 de Ag. de 2017
What you have there already seems to be a working waitbar. How your own functionality fits into that I have no idea, but it is usual to get your code working first and then add a waitbar afterwards rather than add fixed code with a waitbar and try to crowbar your algorithm into that.
Maybe wour waitbar is not updating correctly. Add just after waitbar(step / steps):
updatenow
Samuel Vergara
Samuel Vergara el 24 de Ag. de 2017
Also, about: %%my function goes here, not sure if I need to add something like A(:,:,step)=myFunction(QQ(:,:,step)) As your code is written, you just replace the value of A by the same value "steps" times.

Iniciar sesión para comentar.

Respuestas (1)

Stalin Samuel
Stalin Samuel el 24 de Ag. de 2017
Editada: Stalin Samuel el 24 de Ag. de 2017
I have not found any error in the given code. I tested (Matlab 2017a) the code and wait bar working fine.Instead of your function ( myFunction(QQ) ) i used some delay.Below is the tested code
h = waitbar(0,'Please wait...');
steps=30; %%to extract the number of images inside 3D matrix
for step = 1:steps
pause(0.2) %%removed yuur function
A(:,:,step)=myFunction(QQ(:,:,step))
waitbar(step / steps)
end
close(h)

4 comentarios

Mario
Mario el 24 de Ag. de 2017
Thank you all for your help.
Indeed, your solution @Samuel works with some quick delay.
But for some reason, it is not working with my function and I do not know from where to start to search the problem.
I mean, my input is a 3D stack of images, and my output from that function is also a 3D stack of images.
Should I embed the waitbar into the function somehow?
Thanks!
Steven Lord
Steven Lord el 24 de Ag. de 2017
Define "not working". Does it throw an error (and if so what is the full text of the error message?) Does it issue warnings (and if so, what is the full text of those messages?) Does it do something else that you didn't expect (and if so, describe what it's doing and what you expected it to do in detail.)
Mario
Mario el 25 de Ag. de 2017
The strange thing about it is, for example if my image stack contains 10 images, the waitbar runs the function 10 times(each time incrementing the slider value by 1/10).
In other words, it is making it run multiple times, depending on the total number of images in a 3D stack.
Adam
Adam el 25 de Ag. de 2017
You'd need to post more code. The waitbar doesn't cause anything to happen other than the waitbar to update. Anything else is in the rest of your code.

Iniciar sesión para comentar.

Categorías

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

Preguntada:

el 24 de Ag. de 2017

Comentada:

el 25 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by