关于如何在MATLAB GUI中加入进度条的问题

2025-03-20 22:02:33
推荐回答(2个)
回答(1):

给个参考程序你看看

x = 0;
h = waitbar(x,'请稍等','CreateCancelBtn','delete(gcbf)');
hBtn = findall(h, 'type', 'uicontrol');
set(hBtn, 'string', '取消', 'FontSize', 10);
try
while ishandle(h)
waitbar(x, h, ['当前进度:' num2str(x*100) '%']);
x = x + 0.1;
if x > 1
break
end
pause(1);

end
delete(h);
clear h;
end

回答(2):

function mywaitbar(x,num,varargin)
if nargin < 1
error('Input arguments not valid');
end
fh = varargin{end};
set(0,'CurrentFigure',fh);
fAxestemp = findobj(fh,'type','axes')
fAxes = fAxestemp(num);
set(fh,'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
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(fh,'UserData',fractioninput);
l = line(xline,yline,'EraseMode','none');
set(l,'Color',get(gca,'XColor'));
else
ptemp = findobj(fh,'Type','patch');
p = ptemp(num);
ltemp = findobj(fh,'Type','line');
l = ltemp(num);
if (get(fh,'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;