0

我在 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.
  • 我已验证等待栏进度值不超过 1。
  • 我已经验证等待栏直到在 if/else 循环之外才被关闭。
4

1 回答 1

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); 
于 2012-05-07T18:14:51.653 回答