为什么在Matlab循环中删除了轴?

时间:2016-10-04 12:39:04

标签: matlab error-handling event-handling matlab-figure

试图模仿真实动态条件的代码

clear all; close all; 

hFig2=figure('Units','inches', 'Name', 'Time'); 
hax2=axes(hFig2); 
movegui(hFig2, 'southeast'); 

index=1; 
while (index < 7);
    hFig2=figure(hFig2); 

    u=0:0.01:1+index;
    plot(hax2, u); % Give columns 1xXYZ to Matlab 
    hold on; 
    axis(hax2, 'xy');

    axis(hax2, [0 (size(u,2)/1 - 0) min(u) max(u)]); % to maximise size  
    axis(hax2, 'off'); % no ticks

    index=index+1;
    pause(1); 
    hold off; 
    drawnow    
end;

在更动态的情况下记录1 hax2, 在代码

中主要记录2 hax2
%% Logs 1 in dynamic condition with failure output
% Failure in more dynamic conditions because axes get deleted for some reason
% hax2
% 
% hax2 = 
% 
%   handle to deleted Axes
% 
%% Logs 2 mostly in Code condition and correct because
% hax2 = 
% 
%   Axes with properties:
% 
%              XLim: [0 201]
%              YLim: [0 2]
%            XScale: 'linear'
%            YScale: 'linear'
%     GridLineStyle: '-'
%          Position: [0.1300 0.1100 0.7750 0.8150]
%             Units: 'normalized'

如果由于某种原因hax2 handle to deleted axes失败,则会出错

%% Failure message
%  Show all properties
%
% Warning: MATLAB has disabled some advanced graphics rendering features by switching to software OpenGL. For more information, click
% here. 
% Error using plot
% Invalid handle.
% 
% Error in test_invalid_handle (line 12)
%     plot(hax2, u); 

解决方案的一些初步建议

  1. 在每个循环结束时保存axes句柄;可能的相关主题Save axes handle when plotting In MATLAB
  2. ...
  3. 操作系统:Debian 8.5 64位
    Matlab:2016a
    硬件:华硕Zenbook UX303UA
    Linux内核:4.6的backports

1 个答案:

答案 0 :(得分:2)

调用axes时,第一个输入应该是指定父项的参数/值对。如果您传递单个句柄图形输入,则假定输入是axes句柄

axes(hFig2)

% Error using axes
% Invalid axes handle

或者正如你写的那样

hax2 = axes(hFig2);

% Error using axes
% Too many output arguments.

由于您向其传递了无效的axes句柄,因此它无法正确地将句柄分配给新的axeshax2。您所看到的已删除的hax2可能是来自之前运行的脚本。

相反,您希望使用参数/值对来指定Parent的{​​{1}}属性。

axes

另外,每次循环都会删除对hax2 = axes('Parent', hFig2); 的无关调用,因为你明确指定了每个绘图对象的父对象