How print the loop iteration value each time to show progress of loop during a run

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

You can unsuppress the output, use sprintf or fprintf or disp wherever you need check the values.
For large data sets, you can also put conditional checks in place to display the output if it satisfies the condition(s) put in place.
"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."
In which loop does it get stuck or takes too long time?
If it is in the innermost loop, then it might not be good idea to check the values for each iteration as you will have (atleast) 8*365*266*245 values.
Hi Dyuman,
Thanks a lot, it was the innermost loop and that is the reason i want to check progress but i dont want to print all of the outputs because of the reason you have stated, there are far too many and i dont want to slow it down more. 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? I want the same for all years as well I think would would need to make an array for this and input it into some code.
I had a look into sprintf and i found that i could use continue?
for n = 1:50
if mod(n,7)
continue
end
disp(['Divisible by 7: ' num2str(n)])
end
Is there another way to do it?, as sprintf confused me a little bit; but i will look into it further :)
"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

Sign in to comment.

 Accepted Answer

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'))
k = 12345 at Aug 31 11:15:52
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

Hi Star Strider,
Thanks a lot, do you mean that k is in a for loop and when k reaches a certain value it will print it? I would like to have the 'loop status' almost printed for each year for example like y=1 in the command line or workspace and the time would be useful as well. I am assuming i dont need to change anything apart from k in that code? Would that include the k in the purple? Further as i dont have many outer codes do you know a way i can create a set of values (intervals almost) as an array and use that to reference when the loop reaches a certain value etc.? Maybe it is a bit confusing, but i would appreciate any way to track the progress of my nested for loop.
I would recommend linux with running matlab and especially longer runs, that is what i am currently using
Thanks
Sophia
My pleasure!
In this instance, ‘k1’ (or whatever your index is) will print in every iteration in the loop in which you put it —
tic
for k1 = 1:25
if mod(k1,7) == 0
fprintf('\n\tk1 = %5d at %s\n', k1, datetime('now', 'Format','MMM dd HH:mm:ss'))
end
for k2 = 1:10
v = randn(k1*k2*25);
end
% toc
end
k1 = 7 at Aug 31 12:22:31 k1 = 14 at Aug 31 12:22:33 k1 = 21 at Aug 31 12:22:39
toc
Elapsed time is 15.082995 seconds.
You can also add tic and toc if you want.
EDIT — (30 Aug 2023 at 12:22)
Added mod call and related logic.
.
Hi Star Strider,
Thanks a lot, i looked into tic toc and it will be useful and i will use the first loop of code you suggested. However i dont quite understand what the k2= 1:10 and v does?
Thanks
Sophia
The loops I created simply demonstrate the code I originally posted. Use this approach (or a version of it that works with your code, including changing the format string and variables to be printed) in your own code. This is simply an example to demonstrate what it does (and that it works).
Also, I definitely advise using save to save all the variables you are working on (you can choose which ones) and do that periodically so that you do not lose everything you have already calculated in the event that your computer crashes, since your analysis apparently runs for several days. You can then load the variables from the last time you used save so you do not have to start over from the beginning. (See: How to save data from Genetic Algorithm in case MATLAB crashes? - MATLAB Answers - MATLAB Central for an illustration of how to do that.)
Hi Star Strider,
Thanks a lot, i will adapt and have a go running it with my own code and yes i will definitely save my code!
Thanks
Sophia
My pleasure!
If my Answer helped you solve your problem, please Accept it!
Save your variables!
.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!