Matlab中无效的对象句柄错误 [英] Invalid object handle error in Matlab

查看:129
本文介绍了Matlab中无效的对象句柄错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,是卫星绕地球运动的图形表示.

I have the following code which is a graphic rendering of the movement of a satellite around the Earth.

function ex
global state;
fh = figure('Menu','none','Toolbar','none','Units','characters');
hPanAni = uipanel('parent',fh,'Units','characters','Position',...
    [22.6 10.4 53 23],'title','Controls','FontSize',11,...
    'FontAngle','italic','FontWeight','bold');
hIniAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
    'Position',[0.14 0.75 0.5 0.12],'String','Spin',...
    'FontSize',10,'Callback',@hIniAniCallback);
hFinAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
    'Position',[0.14 0.5 0.5 0.12],'String','Stop',...
    'FontSize',10,'Callback',@hFinAniCallback);
hResetAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
    'Position',[0.14 0.25 0.5 0.12],'String','Reset',...
    'FontSize',10,'Callback',@hResetAniCallback);
hPantSim = uipanel('Parent',fh,'Units','characters',...
    'Position',[107.87 8 157.447 42],'BorderType','none','title',...
    'Screen','FontSize',11,'FontAngle','italic',...
    'FontWeight','bold');
hPantSimInt = uipanel('Parent',hPantSim,'Units','normalized','Position',...
    [0 0 1 1],'BorderType','line','BackgroundColor','black');
ah4 = axes('Parent',hPantSimInt,'Units','normalized','Position',...
    [0 0 1 1],'Color','none','Visible','off','DataAspectRatio',...
    [1 1 1],'NextPlot','add');
rotate3d(ah4);
hgrot = hgtransform('Parent',ah4);
T1 = 0:pi/100:2*pi;
Y = zeros(numel(T1),3);
Y(:,1) = 7000*cos(T1);
Y(:,2) = 7000*sin(T1);
xmin = min(Y(:,1));
ymin = min(Y(:,2));
zmin  = min(Y(:,3));
xmax  = max(Y(:,1));
ymax  = max(Y(:,2));
zmax  = max(Y(:,3));
Resf = 6378;
xmin2 = min(xmin,-Resf);
xmax2  = max(xmax,Resf);
ymin2 = min(ymin,-Resf);
ymax2  = max(ymax,Resf);
zmin2  = min(zmin,-Resf);
zmax2  = max(zmax,Resf);
set(ah4,'XLim',[xmin2 xmax2],'YLim',[ymin2 ymax2],'ZLim',[zmin2 zmax2]);
maptext = imread('tierra.jpg');
[X_im, map] = rgb2ind(maptext,128);
[x_esf,y_esf,z_esf] = sphere(50);
x_esf = Resf*x_esf;
y_esf = Resf*y_esf;
z_esf = Resf*z_esf;
props.FaceColor= 'texture';
props.EdgeColor = 'none';
props.Parent = hgrot;
props.Cdata = flipud(X_im); % it is necesary to do this for getting the 
% appropiate image on the sphere
surface(x_esf,y_esf,z_esf,props);
colormap(map);
axis equal vis3d;
view(3);
az = 0;
Fin = numel(T1);
k = 2;
ind_ini = 0;
state = 0;
handles.tray = zeros(1,Fin);
handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
    'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b');
       function hIniAniCallback(hObject,evt)
            if (ind_ini == 1)
              return;  
            end
            ind_ini = 1;
            state = 0;
            while (k<Fin)
            az = az + 0.01745329252;
            set(hgrot,'Matrix',makehgtform('zrotate',az));
            handles.tray(k) = line([Y(k-1,1) Y(k,1)],[Y(k-1,2) Y(k,2)],...
                [Y(k-1,3) Y(k,3)],...
        'Color','red','LineWidth',3);
            set(handles.psat,'XData',Y(k,1),'YData',Y(k,2),'ZData',Y(k,3));
            pause(0.02); 
            k = k + 1,

            if (state == 1)
                state = 0;
                break;
            end
            end 
       end

    function hFinAniCallback(hObject,evt)
        ind_ini = 0;
        state = 1;
    end
 function hResetAniCallback(hObject,evt)
    set([handles.tray,handles.psat],'Visible','off');
    k = 2;
    ind_ini = 0;
    state = 1;
    az = 0;
    set(hgrot,'Matrix',makehgtform('zrotate',az));
    handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
    'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b');
end

end

但是,当我将其复制到主程序中时,出现以下错误消息:

However, when I copy it to my main program I get the following error message:

??? Error using ==> surface
Invalid object handle

Error in ==> simorbiv8v3>hIniPropCallback at 1911
surface(x_esf,y_esf,z_esf,props);

??? Error while evaluating uicontrol Callback

经过一些证明,我检查了ishandle(props.Parent)是否为false.作为props.Parent = hgrot,问题源是此hgtransform变量.它在主程序中被声明为全局程序,因此不应该出现问题.我不明白hgrot有什么问题.

After some proofs, I have checked that ishandle(props.Parent) is false. As props.Parent = hgrot, the problem source is this hgtransform variable. It is declared as global one in the main program so there should not be problems then. I do not understand what is wrong with hgrot.

推荐答案

我怀疑这是用于Matlab GUI的吗?

I suspect this is for a Matlab GUI?

如果需要将handles变量传递给脚本,通常matlab传递eventdata和hObject以及所有函数,回调等的标准过程.

If so you need to pass the handles variable to the script, normally matlab passes eventdata and hObject as well as a standard procedure for all functions, callbacks and etc.

function ex(hObject, eventdata, handles)

并且不要忘记更新GUI数据以关闭该功能.

And don't forget to update the GUI data to close the function.

guidata(hObject,handles);

我认为您所有使用handles结构的嵌套函数也将需要此功能.

I think all your nested functions that use the handles structure are going to require this as well.

希望这会有所帮助.

这篇关于Matlab中无效的对象句柄错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆