我有以下代码,它是卫星绕地球运动的图形渲染。
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
但是,当我将其复制到我的主程序时,我收到以下错误消息:
??? 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)是否为假。作为props.Parent = hgrot,问题源是这个hgtransform变量。它在主程序中被声明为全局程序,因此不应该存在问题。我不明白hgrot有什么问题。
答案 0 :(得分:1)
我怀疑这是用于Matlab GUI的吗?
如果是这样,你需要将handles变量传递给脚本,通常matlab传递eventdata和hObject以及所有函数,回调等的标准过程。
function ex(hObject, eventdata, handles)
不要忘记更新GUI数据以关闭该功能。
guidata(hObject,handles);
我认为所有使用句柄结构的嵌套函数都需要这个。
希望这有帮助。
答案 1 :(得分:0)
最后,我发现在主程序中有一个cla命令行,它是错误的罪名。