Runtime updating figure contents

2 views (last 30 days)
Chandrasiri Wanayalage
Chandrasiri Wanayalage on 28 Oct 2013
Answered: Prateekshya on 9 Oct 2024
I have a loop running inside the main function for calculations. The function is run by user button click on a figure. I have set a static text to display the number of cycles iterated at each time a loop is completed. But the static text does not update while the loop is running. Only the final number of loops are displayed at the end. But if I run it in debugging mode, the text updates one by one without any problem. How can I see the text is updating while I run the codes?

Answers (1)

Prateekshya
Prateekshya on 9 Oct 2024
Hello Chandrasiri,
In MATLAB, when you are updating a GUI element such as a static text box within a loop, you might encounter an issue where the updates don't appear until the loop finishes. This happens because MATLAB's GUI updates are not automatically flushed to the screen during the loop execution. To force the GUI to update, you need to use the drawnow function.
Here is how you can modify your loop to ensure the static text updates after each iteration:
% Assuming you have a handle to your static text box, e.g., hStaticText
for i = 1:numIterations
% Your calculation code here
% Update the static text with the current iteration number
set(hStaticText, 'String', sprintf('Iteration: %d', i));
% Force MATLAB to update the GUI
drawnow;
end
To know more about this function, please follow this link: https://www.mathworks.com/help/matlab/ref/drawnow.html
I hope this helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!