display intermediate results while the code is running

Hi, I have a code that takes very long time to run (more than 2 days). I want to display intermediate results while the code is running without pausing the code as this will delay the code further. I am not sure how to do this. Please let me know if you have any suggestion. Output can be in the form of .mat/excel/csv.

 Accepted Answer

Lots of ways. For example, you could use fprintf() to print out info to the command window. Or you could do something with a GUI, such as a standalone/floating waitbar/progress bar. Or you could, like I do when I have processing that takes more than about 15 minutes, have a progress bar right on your GUI. Or in your gui, you could just update the gui, like show text that says "Now processing file 1452 of 123487...", etc. What is your preference?

7 Comments

Thank you for your reply. I prefer fprint(). So, what I have is one outer loop that runs for like 1000 iterations and there are two inner loops that run for 200 iterations. I want to display results of outerloop and inner loop. For example when outerloop = 1, what are the values of variables in inner loops 1 and 2.
There is no fprint() but there is an fprintf(). You can do
for k1 = 1 : 1000
fprintf('Now processing iteration #%d...\n', k1);
fprintf('About to start inner loop 1.\n');
for k2 = 1 : 200
fprintf('Beginning first inner loop iteration %d of %d.\n', k2, 200);
% code
end
fprintf('About to start inner loop 2.\n');
for k3 = 1 : 200
fprintf('Beginning second inner loop iteration %d of %d.\n', k3, 200);
% code
end
fprintf('Done with both inner loops in iteration #%d.\n', k1);
end
I am using fprintf() to write my output:
fprintf(fileID1, '%5d %5d %5d %5d %5d\n', k1, k2, history_f.time(k2), rho, a_det ) ;
The issue is when I go to next iteration, the next set of results are getting displayed in the same row. They do not go to the next line. I am not sure what is the issue here, could you please help.
It looks like they're going to a file so you won't see the printout "live". To go to the command window, don't use fileID1 or else use the number 1 instead of fileID1.
If you really want it to go to a file, the most likely problem is the program you're opening it up with, like notepad or wordpad. The first thing to try is to make sure you're opening the file with the 'wt' option, not the 'w' option:
fileID1 = fopen(outputFileName, 'wt');
That should probably work. If that doesn't work, then try writing \n\r or \r\n instead of just \n.
'wt' option works and results are recorded in the next row :).
However, results are not displayed while the code is running especially for k2 loop (ADMM_Release.txt). When I stop the code (Ctrl+C) then I can see results in text file. Not sure whats going on? This is what I have:
fileID1 = fopen('ADMM_Release.txt','wt');
fileID = fopen('ADMM_Fix.txt','wt');
for k1 = 1:1000
for k2 = 1:200
fprintf(fileID1, '%5d %5d %5d %5d %5d\n', k1, k2, time_Release_MIQP, rho, a_det ) ;
end
for k3 = 1:200
fprintf(fileID, '%5d %5d %5d %5d %5d\n', k1, k3, history_f.objval{k3}, time_Fix_LP, rho );
end
end
sorry this is the correct version
for k1 = 1:1000
for k2 = 1:200
fprintf(fileID1, '%5d %5d %5d %5d %5d\n', k1, k2, time_Release_MIQP, rho, a_det ) ;
end
for k3 = 1:200
fprintf(fileID, '%5d %5d %5d %5d %5d\n', k1, k3, history_f.objval{k3}, time_Fix_LP, rho );
end
end
fclose(fileID);
fclose(fileID1);
it is working now. Thanks you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!