我在更新MATLAB GUI中的等待栏时遇到问题。
我创建了一个按预期工作的简单示例。
steps = 5;
hWaitBar = waitbar(0, 'Testing...');
for i = 1:steps
waitbar(i/steps, hWaitBar);
pause(1);
end
close(hWaitBar);
然而,当我在GUI中使用这种结构时......
numSteps = %calculated
hWaitBar = waitbar(0, 'Processing...');
if %conditional
for i = 1:numSteps
waitbar(i/numSteps, hWaitBar)
% additional processing
end
else %conditional
% additional processing
end
close(hWaitBar);
...等待条只能正确显示第一个for循环迭代。
第二次交互因执行失败而失败:
Error using waitbar(109)
Improper arguments for waitbar.
答案 0 :(得分:0)
我找到了解决方案 - 我错过了处理图形。
在原始代码中:
numSteps = %calculated
hWaitBar = waitbar(0, 'Processing...');
if %conditional
for i = 1:numSteps
waitbar(i/numSteps, hWaitBar)
% additional processing
% *** call to imagesc caused the error
end
else %conditional
% additional processing
end
close(hWaitBar);
使用调试器,我看到等待栏成为当前图形,imagesc尝试绘制到它而不是主窗体上的轴。在引用调用之前立即将适当的数字设置为当前行为产生了正确的行为。
更正后的代码:
numSteps = %calculated
hForm = gcf; % save the handle of the main form
hWaitBar = waitbar(0, 'Processing...');
if %conditional
for i = 1:numSteps
% set the waitbar to be the current figure before it is updated
% note: this syntax will ensure window order will be preserved
% with waitbar on top
set(0, 'CurrentFigure', hWaitBar);
waitbar(i/numSteps, hWaitBar);
% additional processing
set(0, 'CurrentFigure', hForm);
imagesc(...);
% more processing
end
else %conditional
% additional processing
end
close(hWaitBar);