- 简单示例说明(只是打印进度)
wait_title = waitbar(0,'Please Wait'); %运行进度条初始化对象
for i = 1 : 100
for j = 1 : 100
%%%%
%代码块
%%%%
str = ['运行中...',num2str(((i-1)*100+j)/10000*100),'%']; %运行进度格式化显示
waitbar(((i-1)*100+j)/10000,wait_title,str); %运行进度显示
end
end
delete(wait_title); %删除进度条
close(wait_title); %关闭进度条
- 复杂示例(同时打印进度和运行时间)
1、主程序
tic;
wait_title = waitbar(0,'Please wait'); %运行进度条初始化
EbN0 = 1:2; %输入比特信噪比 Eb/N0 (dB)
FrameNum =[1 2]; %每个信噪比仿真帧数
for i=1:1:length(snr(:))
for Idx =0:(FrameNum(i)-1)
%%%%
%代码块
%%%%
rate_progress = calculate_progress(i,Idx,EbN0,FrameNum); %计算程序进度
time_out = convert_time(toc); %计算运行时间
str = ['运行进度:',num2str(rate_progress*100),'%','-------','运行时间:',time_out];%运行进度格式化显示
waitbar(rate_progress,wait_title,str) %运行进度显示
end
end
toc;
2、时间转换函数
function time_out = convert_time(time_in)
% 输入toc为double类型,单位s
% 时间转换,输出为char类型xdxhxmxs格式
time_days = fix(time_in/86400);
time_hours = fix((time_in - 86400*time_days)/3600);
time_minutes = fix((time_in - 86400*time_days - 3600*time_hours)/60);
time_seconds = fix(time_in - 86400*time_days - 3600*time_hours - 60*time_minutes);
if time_days == 0
if time_hours == 0
if time_minutes == 0
time_out = [num2str(time_seconds),' ','s'];
else
time_out = [num2str(time_minutes),' ','m',' ',num2str(time_seconds),' ','s'];
end
else
time_out = [num2str(time_hours),' ','h',' ',num2str(time_minutes),' ','m',' ',num2str(time_seconds),' ','s'];
end
else
time_out = [num2str(time_days),' ','d',' ',num2str(time_hours),' ','h',' ',num2str(time_minutes),' ','m',' ',num2str(time_seconds),' ','s'];
end
end
3、进度换算函数
function rate_progress = calculate_progress(i,Idx,EbN0,FrameNum)
%% 计算程序运行进度
if i == 1
rate_progress = (Idx+1)/sum(FrameNum(1:length(EbN0)));
else
rate_progress = (sum(FrameNum(1:i-1))+(Idx+1))/sum(FrameNum(1:length(EbN0)));
end
end