利用Matlab繪制一款專屬進度條
1.使用效果




2.制作歷程
首先我有個程序需要用到進度條,我首先試了一下MATLAB自帶的進度條:
bar=waitbar(0,'讀取數(shù)據(jù)中...'); % waitbar顯示進度條
for i=1:1000
A(i)=rand();
str=['計算中...',num2str(100*i/1000),'%']; % 顯示的文本
waitbar(i/1000,bar,str)
end
但是這樣的進度條顯得冷冰冰的,我就想研究一下其屬性來想辦法看能否對其修飾。
以前的版本系統(tǒng)自帶的進度條可以通過。
set(findobj('type','patch'),'facecolor','b')
這樣的方式改變顏色,但是慢慢的隨著版本更新,這樣的修改方式已經(jīng)不行了,于是我便嘗試查看新版本 waitbar 是怎樣刷新進度條的,我進行了一波 open waitbar 發(fā)現(xiàn)這個函數(shù)就只是創(chuàng)建了figure和axes并不斷更新圖像:

其中更新圖像主要依賴一個名為 uiwaitbar 的函數(shù):

于是我想當然的試了一下 open uiwaitbar 結果發(fā)現(xiàn)打不開。。。

于是我根據(jù) waitbar.m 的位置順藤摸瓜的在:toolbox\matlab\uitools\private
路徑找到了 uiwaitbar.p 文件,啊加密文件,那沒事了嗷:

雖然是加密文件,但我們還是通過換著參數(shù)調(diào)用發(fā)現(xiàn)這個函數(shù)可能需要用到一些png或者css文件:

不過本人css用的確實不夠熟,同時也比較難看出加密文件到底是怎么調(diào)用css文件的,但是總結來說waitbar就只是一個不斷刷新圖像的figure而已,我們自己寫的話甚至可以不用css或者png,直接用自帶的畫圖函數(shù)都可以。
3.函數(shù)用法
寫了以下幾個進度條函數(shù)(四種風格):
- waitBar_SL1.m(僅支持英文)
- waitBar_SL2.m
- waitBar_SL3.m
- waitBar_SL4.m
基本用法like this:
bar=waitBar_SL2(0,'loading ...'); % 初始化
for i=1:1000
A(i)=rand();
str=['precess - ',num2str(round(i/10)),'%']; % 顯示的文本
waitBar_SL2(bar,i/1000,str);
end4.工具函數(shù)完整代碼
waitBar_SL1.m

function barHdl=waitBar_SL1(varargin)
% @author:slandarer
%
% try this Code:
% --------------------------
% bar=waitBar_SL1(0,'loading ...'); % 初始化
%
% len=1000;
% for i=1:len
% A(i)=rand();
% str=['progress - ',num2str(round(i/len*100)),'%']; % 顯示的文本
% waitBar_SL1(bar,i/len,str);
% end
% 第一次調(diào)用先創(chuàng)建figure和axes
if ~strcmp(get(varargin{1},'type'),'figure')
screenSize=get(0,'ScreenSize');
width=screenSize(3)*0.24;
height=screenSize(4)*0.12;
pos=[screenSize(3)/2-width/2 screenSize(4)/2-height/2 width height];
barHdl=figure();
barHdl.Position=pos;
barHdl.Resize='off';
barHdl.Name='waitbar-slandarer-type1';
barHdl.NumberTitle='off';
barHdl.IntegerHandle='off';
barHdl.MenuBar='none';
barHdl.Interruptible='off';
barHdl.DockControls='off';
barAx=axes('Parent',barHdl);
barAx.Position=[0 0 1,1];
barAx.Color=[0.99 0.96 0.95];
barAx.XColor='none';
barAx.YColor='none';
barAx.XLim=[0,105];
barAx.YLim=[0,60];
barAx.XGrid='on';
barAx.YGrid='on';
barAx.XTick=0:5:105;
barAx.YTick=0:5:60;
barAx.GridColor=[0.71 0.78 0.86];
barAx.GridAlpha=0.3;
barAx.LineWidth=1.2;
hold(barAx,'on')
rectangle(barAx,'Position',[0 0 105 38],'Curvature',0.3,'FaceColor',[0.4 0.5 1 .2],...
'LineWidth',1.5,'EdgeColor',[0.16 0.15 0.65],'AlignVertexCenters','on')
rectangle(barAx,'Position',[2.5 4 100 30],'Curvature',0.1,'FaceColor',[1 1 1 .8],...
'LineWidth',1.5,'EdgeColor',[0.16 0.15 0.65],'AlignVertexCenters','on')
barAx.UserData.Title=text(barAx,105/2,50,varargin{2},'horizontalAlignment','center',...
'FontSize',14,'FontWeight','bold','FontName','Comic Sans MS','Color',[0.16 0.15 0.65]);
barAx.UserData.RateHdl=plot(barAx,[],[],'Color',[0.16 0.15 0.65],'LineWidth',2,'Tag','rateHdl');
drawnow
else
barHdl=varargin{1};
barAx=barHdl.Children;
barAx.UserData.Title.String=varargin{3};
rate=round(varargin{2}*100/5);
if rate>0
X=(1:rate).*5;
Y=ones(1,rate);
XSet=[X-2.5;X+2.5];
YSet=[Y.*4.5;Y.*33.5];
delete(findobj('Tag','rateHdl'));
barAx.UserData.RateHdl=plot(barAx,XSet,YSet,'Color',[0.16 0.15 0.65],'LineWidth',2,'Tag','rateHdl');
end
drawnow
end
endwaitBar_SL2.m

function barHdl=waitBar_SL2(varargin)
% @author:slandarer
%
% try this Code:
% --------------------------
% bar=waitBar_SL2(0,'loading ...'); % 初始化
%
% len=1000;
% for i=1:len
% A(i)=rand();
% str=['progress - ',num2str(round(i/len*100)),'%']; % 顯示的文本
% waitBar_SL2(bar,i/len,str);
% end
xyMin=[5,10];
xyMax=[95,25];
% 第一次調(diào)用先創(chuàng)建figure和axes
if ~strcmp(get(varargin{1},'type'),'figure')
screenSize=get(0,'ScreenSize');
width=screenSize(3)*0.24;
height=screenSize(4)*0.12;
pos=[screenSize(3)/2-width/2 screenSize(4)/2-height/2 width height];
barHdl=figure();
barHdl.Position=pos;
barHdl.Resize='off';
barHdl.Name='waitbar-slandarer-type2';
barHdl.NumberTitle='off';
barHdl.IntegerHandle='off';
barHdl.MenuBar='none';
barHdl.Interruptible='off';
barHdl.DockControls='off';
barAx=axes('Parent',barHdl);
barAx.Position=[0 0 1,1];
barAx.Color=[0.99 0.96 0.95];
barAx.XColor='none';
barAx.YColor='none';
barAx.XLim=[0,100];
barAx.YLim=[0,50];
hold(barAx,'on')
%0.8200 0.3300 0.1200
fill(barAx,[xyMin(1),xyMax(1),xyMax(1),xyMin(1)],...
[xyMin(2),xyMin(2),xyMax(2),xyMax(2)],[0.85 0.4 0.13].*1.1);
xSep1=5;
xSep2=3;
for i=1:9
fill(barAx,[xyMin(1),xyMin(1)+xSep1,xyMin(1)+xSep1+xSep2,xyMin(1)+xSep2]+(i-1)*10.2,...
[xyMin(2),xyMin(2),xyMax(2),xyMax(2)],[0.82 0.33 0.12],'EdgeColor','none')
end
barAx.UserData.Title=text(barAx,5,37.5,varargin{2},'horizontalAlignment','left',...
'FontSize',14,'FontWeight','bold','Color',[0.2 0.2 0.2]);
barAx.UserData.RateHdl=fill(barAx,[xyMin(1),xyMax(1),xyMax(1),xyMin(1)],...
[xyMin(2),xyMin(2),xyMax(2),xyMax(2)],...
[0.99 0.96 0.95],'EdgeColor','none');
plot(barAx,[xyMin(1),xyMin(1)]-0.8,[xyMin(2),xyMax(2)],'Color',[0.2,0.2,0.2],'LineWidth',3)
plot(barAx,[xyMax(1),xyMax(1)]+0.8,[xyMin(2),xyMax(2)],'Color',[0.2,0.2,0.2],'LineWidth',3)
plot(barAx,[xyMin(1),xyMax(1)],[xyMin(2),xyMin(2)]-1,'Color',[0.2,0.2,0.2],'LineWidth',3)
plot(barAx,[xyMin(1),xyMax(1)],[xyMax(2),xyMax(2)]+1,'Color',[0.2,0.2,0.2],'LineWidth',3)
drawnow
else
barHdl=varargin{1};
barAx=barHdl.Children;
barAx.UserData.Title.String=varargin{3};
rate=varargin{2};
xMin=rate*(xyMax(1)-xyMin(1))+xyMin(1);
barAx.UserData.RateHdl.XData=[xMin,xyMax(1),xyMax(1),xMin];
drawnow
end
endwaitBar_SL3.m

function barHdl=waitBar_SL3(varargin)
% @author:slandarer
%
% try this Code:
% --------------------------
% bar=waitBar_SL3(0,'loading ...'); % 初始化
%
% len=1000;
% for i=1:len
% A(i)=rand();
% str=['progress - ',num2str(round(i/len*100)),'%']; % 顯示的文本
% waitBar_SL3(bar,i/len,str);
% end
xyMin=[5,10];
xyMax=[95,25];
% 第一次調(diào)用先創(chuàng)建figure和axes
if ~strcmp(get(varargin{1},'type'),'figure')
screenSize=get(0,'ScreenSize');
width=screenSize(3)*0.24;
height=screenSize(4)*0.12;
pos=[screenSize(3)/2-width/2 screenSize(4)/2-height/2 width height];
barHdl=figure();
barHdl.Position=pos;
barHdl.Resize='off';
barHdl.Name='waitbar-slandarer-type3';
barHdl.NumberTitle='off';
barHdl.IntegerHandle='off';
barHdl.MenuBar='none';
barHdl.Interruptible='off';
barHdl.DockControls='off';
barAx=axes('Parent',barHdl);
barAx.Position=[0 0 1,1];
barAx.Color=[0.99 0.96 0.95];
barAx.XColor='none';
barAx.YColor='none';
barAx.XLim=[0,100];
barAx.YLim=[0,50];
hold(barAx,'on')
%0.8200 0.3300 0.1200
fill(barAx,[xyMin(1),xyMax(1),xyMax(1),xyMin(1)],...
[xyMin(2),xyMin(2),xyMax(2),xyMax(2)],[0.4100 0.6200 0.15].*1.3);
xSep1=5;
xSep2=3;
for i=1:9
fill(barAx,[xyMin(1),xyMin(1)+xSep1,xyMin(1)+xSep1+xSep2,xyMin(1)+xSep2]+(i-1)*10.2,...
[xyMin(2),xyMin(2),xyMax(2),xyMax(2)],[0.47 0.66 0.12],'EdgeColor','none')
end
barAx.UserData.Title=text(barAx,5,37.5,varargin{2},'horizontalAlignment','left',...
'FontSize',14,'FontWeight','bold','Color',[0.2 0.2 0.2]);
barAx.UserData.RateHdl=fill(barAx,[xyMin(1),xyMax(1),xyMax(1),xyMin(1)],...
[xyMin(2),xyMin(2),xyMax(2),xyMax(2)],...
[0.99 0.96 0.95],'EdgeColor','none');
plot(barAx,[xyMin(1),xyMin(1)]-0.8,[xyMin(2),xyMax(2)],'Color',[0.2,0.2,0.2],'LineWidth',3)
plot(barAx,[xyMax(1),xyMax(1)]+0.8,[xyMin(2),xyMax(2)],'Color',[0.2,0.2,0.2],'LineWidth',3)
plot(barAx,[xyMin(1),xyMax(1)],[xyMin(2),xyMin(2)]-1,'Color',[0.2,0.2,0.2],'LineWidth',3)
plot(barAx,[xyMin(1),xyMax(1)],[xyMax(2),xyMax(2)]+1,'Color',[0.2,0.2,0.2],'LineWidth',3)
drawnow
else
barHdl=varargin{1};
barAx=barHdl.Children;
barAx.UserData.Title.String=varargin{3};
rate=varargin{2};
xMin=rate*(xyMax(1)-xyMin(1))+xyMin(1);
barAx.UserData.RateHdl.XData=[xMin,xyMax(1),xyMax(1),xMin];
drawnow
end
endwaitBar_SL4.m

function barHdl=waitBar_SL4(varargin)
% @author:slandarer
%
% try this Code:
% --------------------------
% bar=waitBar_SL4(0,'loading ...'); % 初始化
%
% len=1000;
% for i=1:len
% A(i)=rand();
% str=['progress - ',num2str(round(i/len*100)),'%']; % 顯示的文本
% waitBar_SL4(bar,i/len,str);
% end
% 第一次調(diào)用先創(chuàng)建figure和axes
if ~strcmp(get(varargin{1},'type'),'figure')
screenSize=get(0,'ScreenSize');
width=screenSize(3)*0.18;
height=screenSize(3)*0.18;
pos=[screenSize(3)/2-width/2 screenSize(4)/2-height/2 width height];
barHdl=figure();
barHdl.Position=pos;
barHdl.Resize='off';
barHdl.Name='waitbar-type4';
barHdl.NumberTitle='off';
barHdl.IntegerHandle='off';
barHdl.MenuBar='none';
barHdl.Interruptible='off';
barHdl.DockControls='off';
barAx=axes('Parent',barHdl);
barAx.Position=[0 0 1,1];
barAx.Color=[1 1 1];
barAx.XColor='none';
barAx.YColor='none';
barAx.XLim=[0,100];
barAx.YLim=[0,100];
hold(barAx,'on')
t=linspace(0,-2*pi,pi/(pi/500))+pi/2;
xSet=cos(t);
ySet=sin(t);
fill(barAx,[xSet.*35,xSet(end:-1:1).*45]+50,...
[ySet.*35,ySet(end:-1:1).*45]+50,[1 1 1].*0.93,'EdgeColor','none');
barAx.UserData.Title=text(barAx,50,50,varargin{2},'horizontalAlignment','center',...
'FontSize',14,'FontWeight','bold','Color',[0.2 0.2 0.2]);
barAx.UserData.RateHdl=fill(barAx,[0 0 0 0],...
[0 0 0 0],...
[0.53 0.81 0.93],'EdgeColor','none');
drawnow
else
barHdl=varargin{1};
barAx=barHdl.Children;
barAx.UserData.Title.String=varargin{3};
rate=varargin{2}*2*pi;
t=linspace(0,-rate,rate/(pi/500))+pi/2;
xSet=cos(t);
ySet=sin(t);
barAx.UserData.RateHdl.XData=[xSet.*35,xSet(end:-1:1).*45]+50;
barAx.UserData.RateHdl.YData=[ySet.*35,ySet(end:-1:1).*45]+50;
drawnow
end
end5.下載地址
以上就是利用Matlab繪制一款專屬進度條的詳細內(nèi)容,更多關于Matlab進度條的資料請關注腳本之家其它相關文章!
相關文章
VS2017+Qt5+Opencv3.4調(diào)用攝像頭拍照并存儲
本文主要介紹了VS2017+Qt5+Opencv3.4調(diào)用攝像頭拍照并存儲,實現(xiàn)了視頻,拍照,保存這三個功能。具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
C++開發(fā)的Redis數(shù)據(jù)導入工具優(yōu)化
這篇文章主要介紹了C++開發(fā)的Redis數(shù)據(jù)導入工具優(yōu)化方法的相關資料,需要的朋友可以參考下2015-07-07

