为什么在Matlab循环中删除轴句柄



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

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;

log 1 hax2在更动态的情况下,日志2 hax2主要在Code

%% 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'

hax2handle 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句柄;在MATLAB中绘制时保存轴句柄

操作系统:Debian 8.5 64位
Matlab: 2016
硬件:华硕Zenbook UX303UA
Linux kernel: 4.6 of backports

调用axes时,第一个输入应该是指定父对象的参数/值对。如果你给它传递一个单句柄图形输入,它会假设输入是一个axes

句柄
axes(hFig2)
% Error using axes
% Invalid axes handle

或者写成

hax2 = axes(hFig2);
% Error using axes
% Too many output arguments.

因为你传递了一个无效的axes句柄给它,它不能正确地将一个新的axes句柄分配给hax2。您看到的已删除的hax2很可能来自先前的脚本运行。

相反,您需要使用参数/值对来指定axesParent属性。

hax2 = axes('Parent', hFig2);

另外,由于您显式地指定了每个plot对象

的父对象,因此我将每次通过循环删除对figure的无关调用。

相关内容

  • 没有找到相关文章