Matlab command line progress bar 字符界面进度条

Version 1.0.1 (3.09 KB) by Mingqi
command line progress bars, both simple, and complex with time estimation
7 Downloads
Updated 27 Feb 2025

View License

There are two functions and one demo script in the attached zip file.
1. Simple progress bar 简单进度条
function [s] = mat_progress_bar(front_text, current_index, max_index)
%MAT_PROGRESS_BAR
% Input:
% - front_text: a char array, the text before the progress bar
% - current_index: index of current loop
% - max_index: total number of the loop
%
% Author: Mingqi Zhao, Lanzhou University, China
%
%
total_bar_num = 20;
if(max_index <= total_bar_num)
rep_num = round(total_bar_num/max_index);
bar_text = repmat('=',1,rep_num);
bar_flag = 1;
else
bar_text = '=';
bar_flag = ceil(max_index/total_bar_num);
end
percent_done = 100 * current_index / max_index;
previous_percent_done = 100 * (current_index-1) / max_index;
previous_perc_msg = ['>',num2str(previous_percent_done,'%3.1f'), '%%'];
perc_msg = ['>',num2str(percent_done,'%3.1f'), '%%'];
s_previous_perc_msg = sprintf(previous_perc_msg);
backspace_len = length(s_previous_perc_msg);
backspace_matrix = repmat(sprintf('\b'), 1, backspace_len);
if(current_index == 1)
s = [front_text,': |', perc_msg];
fprintf(s);
elseif(current_index > 1 && current_index < max_index)
if(rem(current_index, bar_flag) == 0)
s = [backspace_matrix, bar_text, perc_msg];
fprintf(s);
else
s = [backspace_matrix, perc_msg];
fprintf(s);
end
else
if(rem(current_index, bar_flag) == 0)
s = [backspace_matrix, bar_text, perc_msg, '\n'];
fprintf(s);
else
s = [backspace_matrix, perc_msg, '\n'];
fprintf(s);
end
end
end
2. Complex progress bar 复杂进度条
function [ previous_text_len ] = mat_progress_bar_t(varargin)
%MAT_PROGRESS_BAR_T
% Input:
% - front_text: a char array, the text before the progress bar
% - current_index: index of current loop
% - max_index: total number of the loop
% - t: time estimation of current loop, acquired by tic and t=toc,
% see demo
% - previous_text_len: length of text in previous loop, acquired by
% this function in previous loop
%
% Output:
% - previous_text_len: text length of this loop for the function call
% of next loop, see demo for the usage.
%
% Author: Mingqi Zhao, Lanzhou University, China
%
%
persistent total_t
marker_solid = '#';
%marker = '#';
marker_empty = '-';
%marker_empty = ' ';
%% dealwith input arguments
if(nargin == 5)
front_text = varargin{1};
current_index = varargin{2};
max_index = varargin{3};
t = varargin{4};
previous_text_len = varargin{5};
write_text_file = 0;
elseif(nargin == 6)
front_text = varargin{1};
current_index = varargin{2};
max_index = varargin{3};
t = varargin{4};
previous_text_len = varargin{5};
fid = varargin{6};
write_text_file = 1;
else
end
if(isempty(total_t))
total_t = 0;
else
total_t = total_t + t;
end
%% this remaining time
remain_t = t*(max_index - current_index);
remain_t_h = floor(remain_t/3600);
remain_t_m = floor((remain_t - remain_t_h*3600)/60);
remain_t_s = round(remain_t - remain_t_h*3600 - remain_t_m*60);
remain_t_str = [' [RT: ',num2str(remain_t_h), 'h ', num2str(remain_t_m), 'min ', num2str(remain_t_s,'%02d'), 's]'];
%% total time
total_t_h = floor(total_t/3600);
total_t_m = floor((total_t - total_t_h*3600)/60);
total_t_s = round(total_t - total_t_h*3600 - total_t_m*60);
total_t_str = [' [UT: ', num2str(total_t_h), 'h ', num2str(total_t_m), 'min ', num2str(total_t_s, '%02d'), 's]'];
%% this index info
index_str = [' [Loop: ', num2str(current_index), '/', num2str(max_index), ']'];
%% percentage
percent_done = 100 * current_index / max_index;
perc_str = num2str(percent_done,'%3.1f');
%% prepare bar str
total_bar_num = 20;
prc_step = 100/total_bar_num;
dot_num = round(percent_done./prc_step);
bar_str = ['[', repmat(marker_solid, 1, dot_num), repmat(marker_empty, 1, total_bar_num-dot_num), '] '];
total_str = [front_text, ': ', bar_str, perc_str, '%%', index_str, remain_t_str, total_t_str];
if(current_index == 1)
s = total_str ;
elseif(current_index > 1 && current_index < max_index)
backspace_matrix = repmat(sprintf('\b'), 1, previous_text_len);
s =[backspace_matrix, total_str];
elseif(current_index == max_index)
backspace_matrix = repmat(sprintf('\b'), 1, previous_text_len);
s = [backspace_matrix(1:end), total_str, '\n'];
end
previous_text_len = length(sprintf(total_str));
%% plot bar
fprintf(s);
if(write_text_file)
fprintf(fid, s);
end
end
3. Demo script 测试程序
%%
% Demo code of mat_progress_bar and mat_progress_bar_t
%
% Mingqi Zhao, Lanzhou University, China
clear;clc;
loop_n = 1000; % number of simulated loop
delay = 0.01; % unit time consumption of the simulated task
%% A demo of simple bar
for iter=1:1:loop_n
% here do your loop work
pause(delay);
mat_progress_bar('Simple bar', iter, loop_n);
end
%% A demo of complex bar with remaining time (RT) and used time (UT) estimation
bar_start = 0;
for iter=1:1:loop_n
tic;
% here do your loop work
pause(delay);
%
%
t=toc;
bar_start = mat_progress_bar_t('Complex bar', iter, loop_n, t, bar_start);
end
4. Screen shot of the demo run 截图展示

Cite As

Mingqi (2025). Matlab command line progress bar 字符界面进度条 (https://www.mathworks.com/matlabcentral/fileexchange/180249-matlab-command-line-progress-bar), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2024b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.1

Added Chinese explanation

1.0.0