How print the loop iteration value each time to show progress of loop during a run
15 views (last 30 days)
Show older comments
Hi All,
I am running a large dataset with various calculations for 4 dimensions (year (8), days (365), lat (266) and lon (245)) and i think it will take a really long time to run (maybe a few days), however during my debugging i found that sometimes the code would not move onto another iterations or take too long when there was an error. Therefore i want to see its progress in real time if possible, my loop is set out as for 1:8, dim2 (for 1:365), dim3.. etc. Is there a way of printing the loop iteration each time in the workspace? or maybe select a specific loop iteration i.e. year instead of selecting all of them? Let me know it will really help me track the progress of my run!
4 Comments
Dyuman Joshi
on 31 Aug 2023
"I was thinking therefore if i could put intervals for it, 1:365 e.g. day 100, 200 and 300 etc? and then if it is passed it could print that as it=100 etc?"
That will definitely work.
"I had a look into sprintf and i found that i could use continue?"
No need to use continue here.
for n = 1:50
if mod(n,7)
disp(['Divisible by 7: ' num2str(n)]);
end
end
Also, since you have a big number of iterations, you might benefit from vectorizing part(s) of code - Vectorization
Accepted Answer
Star Strider
on 31 Aug 2023
I am not certain what you want to do, however when I have done something similar, I usually do something like this:
k = 12345;
fprintf('\n\tk = %d at %s\n', k, datetime('now', 'Format','MMM dd HH:mm:ss'))
Put it in an appropriate place in an outer loop so that it does not print out too often and slow the code. Put in as much detail and as many variables to print as necessary.
Also, if your code is going to run for days, be sure to save all the important interim results to a .mat file in one of the outer loops so that you do not lose everything if your computer crashes. You can then pick up where you left off by loading the appropriate saved file. (Windows 11 seems to crash frequently for the past several months, always without warning. I am gong to swittch to Linux as soon as I learn enough about it.)
.
8 Comments
More Answers (0)
See Also
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!