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.
采纳的回答
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 个评论
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!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
