最近用到了waitbar,但是Matlab的waitbar总游离于主窗口之外,看起总是不是那么地爽于是想在论坛上找到把进度条嵌入到主GUI界面去的方法,不过没有找到现成的,只好自己动手了^_~ 在GUI主窗口找一个空地,放上一个axes,然后如下设置这个axes的参数属性: 'XLim',[0 100], 'YLim',[0 1], 'Box','on', 'Units','Points', 'XTickMode','manual', 'YTickMode','manual', 'XTick',[], 'YTick',[], 'XTickLabelMode','manual', 'XTickLabel',[], 'YTickLabelMode','manual', 'YTickLabel',[]); 偶根据liuhuanjinliu大哥的思想改编一下waitbar为自定义函数mywaitbar(偶简去了原waitbar函数的其它功能,呵呵。。。) mywaitbar的代码如下: -
- function mywaitbar(x,varargin)
- if nargin < 1
- error('Input arguments not valid');
- end
- set(0,'CurrentFigure',gcf);
- fAxes = findobj(gcf,'type','axes');
- set(gcf,'CurrentAxes',fAxes);
- if nargin > 1
- hTitle = get(fAxes,'title');
- set(hTitle,'String',varargin{1});
- end
- fractioninput = x;
- x = max(0,min(100*x,100));
- if fractioninput == 0
- cla
- pause(0.1) % 暂停小会清除上次使用进度
- xpatch = [0 x x 0];
- ypatch = [0 0 1 1];
- xline = [100 0 0 100 100];
- yline = [0 0 1 1 0];
- patch(xpatch,ypatch,'b','EdgeColor','b','EraseMode','none');
- set(gcf,'UserData',fractioninput);
- l = line(xline,yline,'EraseMode','none');
- set(l,'Color',get(gca,'XColor'));
- else
- p = findobj(gcf,'Type','patch');
- l = findobj(gcf,'Type','line');
- if (get(gcf,'UserData') > fractioninput)
- set(p,'EraseMode','normal');
- end
- xpatch = [0 x x 0];
- set(p,'XData',xpatch);
- xline = get(l,'XData');
- set(l,'XData',xline);
- end
- drawnow;
-
复制代码 GUI主界面某个回调函数下: -
- mywaitbar(0,'Please Wait...');
- TheEndTime = 600;
- for t = 1:TheEndTime
- mywaitbar(t/TheEndTime,[num2str(floor(t*100/TheEndTime)),'%']);
- end
-
复制代码 |