MATLAB Answers

If I have two spheres plotted (one inside the other), how can I rotate only the interior one?

1 view (last 30 days)
Chris Gnam
Chris Gnam on 28 May 2016
Commented: Chris Gnam on 28 May 2016
Hi guys, so I'm currently working on a code which generates a model of the earth, plots your position on the earth, and then a sphere of stars around it. I am able to make this plot, and I am also able to get the earth to rotate when plotted alone (using the rotate function). However, whenever I try to plot both and then rotate the earth either the outer sphere doesn't get plotted, or both are plotted but I get the following error:
Error using rotate (line 29)
H must contain axes children only.
I'm really not sure what this error means, and I don't know how to fix it... if you need to look at my code, here it is:
%%Request the user for GPS Coordinates:
Lat = input('Please input the Lattitude of your desired Location: ')
Long = input('Please input the Longitude of your desired Location: ')
%%Generate the sphere that will later become Earth
alt = 1;
skyDome = figure;
set(skyDome,'ToolBar','none','MenuBar','none','name','Model of Sky','numbertitle','off',...
'units','normalized','outerposition',[0 .05 1 .95]);
positionGlobe = plot3(0,0,1,'.','markersize',20,'color','red'); %Place point on earth's surface
[x y z] = sphere(128);
r = 5;
stars = surfl(r*x, r*y, r*z);
set(stars, 'FaceAlpha', .25)
shading interp
%%Import an image of the Earth and map it to the sphere
img = imread('Earth Texture.jpg');
[imgInd,map] = rgb2ind(img,256);
[imgIndRows,imgIndCols] = size(imgInd);
[X,Y,Z] = sphere(imgIndRows,imgIndCols);
%%Reproduce Earth's axial tilt
earth = findobj(gca,'type','surface');
%Define the prime meridian/equator intersection
rotate(positionGlobe,[1 0 0],90); %Align with prime meridian
rotate(positionGlobe,[0 0 1],90); %Align with equator
%Input GPS coordinates:
rotate(positionGlobe,[0 1 0],-Lat);
rotate(positionGlobe,[0 0 1],Long);
rotate(earth,[0 1 0],23.6);
rotate(positionGlobe,[0 1 0],23.6); %Orient coordinates along axial North Pole
%%Produce a standard map to show position location
hold on
positionMap = plot(800+4.444444*(Long),400-4.4444444*(Lat),'.','markersize',20,'color','red'); %Display point on map
positionData = text(0, 820,['Latitude: ', num2str(Lat), char(176), ' | Longitude: ', num2str(Long), char(176),...
' | Time of Day: '],'Fontsize',12)
%%Spin the model of the earth
for i = 1:1000
hold on
deg = 1;
rotate(earth,[.22 0 .5],deg);
rotate(positionGlobe,[.22 0 .5],deg);
M(i) = getframe(gca);
set(positionData,'string',['Latitude: ', num2str(Lat), char(176), ' | Longitude: ', num2str(Long), char(176),...
' | Time of Day: ',num2str(((deg/360)*24)*i)])
Any help would be greatly appreciated!

Accepted Answer

Walter Roberson
Walter Roberson on 28 May 2016
You have
earth = findobj(gca,'type','surface');
That is risky, as the object might be part of a different axes. It would be safer to assign the result of the surface() call to earth and not do the findobj(). As it is now, there is the possibility that earth will come out as the empty graphics placeholder.
  1 Comment
Chris Gnam
Chris Gnam on 28 May 2016
Yeah, I actually tried that on my flight home today and I was able to make it work. Thanks for the help friend!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by