前言
在循环程序中,常常需要通过打印索引数值来获得程序运行的进度。但是如果循环次数很多时,指令窗口就会覆盖之前的历史记录。 这里分享如何只在一行打印当前三重循环的进度。
代码
process_count = 0; % 起始的字符计数
i_counts = 10;
j_counts = 10;
k_counts = 10;
sum_loop = i_counts*j_counts*k_counts; % 总循环数
for index_i = 1:i_counts
for index_j = 1:j_counts
for index_k = 1:k_counts
%获取当前索引值
index_current = (index_i-1)* j_counts*k_counts + ...
(index_j -1 )* k_counts + index_k;
fprintf(repmat('\b',1,process_count)); % \b 是需要退格的数量
process_count=fprintf('current progress:%.4f', (index_current/sum_loop));
pause(0.01)
end
end
end