MATLAB: Invalid object handle error

handleMATLABobject

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
I do not understand what is happening.

Best Answer

Command
dbstop if error
and then run the code. When it stops at that line, examine props.Parent and in particular check ishandle(props.Parent) . If it says that it is not a handle, chase further up to ah4 and make sure that it is still a handle, then up to hPantSimInt and then to hPantSim and then to fh