How can I make the Earth rotate around it's axis in matlab? - matlab

So I want to represent the Earth rotating for a number of seconds from a tspan knowing that a full rotation happens in 86160 seconds. It means that for 239.33 seconds , the Earth rotates with a degree. The problem is that I don t know how to use the rotate command in a right way.
This is the code:
tspan=[0 :72000];
[X,Y,Z]=sphere(50);
R=6400000;
earth = imread('earth.jpg');
globe= surf(-X*R,Y*R,-Z*R);
image_file='earth.jpg';
cdata = imread(image_file);
set(globe, 'FaceColor', 'texturemap', 'CData', cdata, 'EdgeColor', 'none');
set(gcf,'Color','k')
set(gca, 'visible', 'off')
axis equal
view (90,0)
rotating=1; % 1 degree ever 239.33 seconds from tspan
rotate(earth, [0 1 0],1) % test to see if it's working

instead of rotating the earth, rotate the camera using view, try
...
axis equal
for n=0:90
view(90+n,0)
pause(1)
end

Related

How to plot point and vectors in Matlab

A = [239920.240412166 1.31193313030682;
243577.444235102 1.38185205485119;
241899.250050298 1.51264147493485;
244659.326936560 1.50845243215867;
239862.361809342 1.50810833389632;
238395.616682194 1.37125000688350;
244558.389789124 1.27212093329482;
244290.890880318 1.35116080948488;
240303.711239396 1.36064181572699;
237464.430450140 1.48857869573721;
244415.381196104 1.51252425335623;
239855.328594799 1.29178640586301;
239304.806448742 1.31075813783171;
244827.243024016 1.32080934043223;
241465.885648910 1.53667019314427;
241139.254464482 1.40424079027764;
242300.037630214 1.27160249886092;
243330.396959248 1.61411410292679;
237530.389940994 1.21846939260826];
B = [0.6 0.18; 0.15 0.46]; % green circles
for i=1:2
plot(A(:,1),A(:,2),'r*');
hold on
plot(B(i,1),B(i,2), '-ko',...
'LineWidth',1,...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',9);
end
When I ploted A and B, I got this:
B(1,1) = 0.6, but it appears in 0 (X-axis). Same with B(2,1) = 0.15
How to correct this?
A logarithmic scale on the xaxis will help with the view
set(gca, 'XScale', 'log')
However, it will lead to the fact, that now the values of A appear to populate one vertical line.
If you cannot live with this, you may want to try a broken x-axis. MATLAB doesn't support this with build-in functions, but there is a solution in the MATLAB file exchange
https://de.mathworks.com/matlabcentral/fileexchange/3683-breakxaxis
Btw: There is no need for the loop in your code. In fact you plot A twice on top of each other. Just
% Plot A and B without loop
plot(A(:,1), A(:,2),'r*')
hold on
plot(B(:,1), B(:,2), '-ko', 'LineWidth', 1, ...
'MarkerFaceColor', [.49 1 .63], 'MarkerSize',9)
% Set x axis to logarithmic scale
set(gca, 'XScale', 'log')
is sufficient to display your plot
Your x-axis goes from 0 to 250000. On that range, 0.6 and 0.15 are practically 0.
If you want you can use logarithmic scale in x-axis using semilogx

How to continuously update 2 plots and plotted Camera in same figure (MATLAB)

My goal is to continuously plot the position & orientation of camera relative to marker using MATLAB.
There are three thing to plot.(1)camera (2)all circle points (3)origin point '*'
Both the camera and points will be moving in each frame.I plotted all these three things in a static figure i.e. using hold on. as shown in the attached figure.
Now i want to plot them all continuously (real time) in the same figure as the values change.Till now i have only been able to dynamically update only one of these things i.e. the circular points for some random values. If I add another plot ,it conflicts.Can you please tell me how to update multiple plots in same figure and the plotCamera.
hF = figure;
hAx = gca;
im_pt_2world=rand(3,3);
x_o=im_pt_2world(1,1); y_o=im_pt_2world(1,2); %origin of the calibrated world points
x_ip=im_pt_2world(:,1); y_ip=im_pt_2world(:,2);
hpoints = plot3(x_ip, y_ip,zeros(size(im_pt_2world, 1),1),'ro');
% I added the "ishandle" so the program will end in case u closed the figure
while (1) & ishandle(hpoints)
%instead of using plot I directly change the data in the line
% this is faster the plot if only because you don't need to reedefine the limits and labels...
im_pt_2world=rand(3,3);
x_ip=im_pt_2world(:,1); y_ip=im_pt_2world(:,2);
set(hpoints,'ydata',y_ip);
set(hpoints,'xdata',x_ip);
drawnow %updates the display
end
The plotCamera function (Computer Vision System Toolbox) returns a handle to the graphical object, which you can manipulate programmatically. Changing the object's properties, such as Location and Orientation will move the camera in the plot. The help example for plotCamera shows how to make the camera fly in a circle:
% Plot a camera pointing along the Y-axis
R = [1 0 0;
0 0 -1;
0 1 0];
% Setting opacity of the camera to zero for faster animation.
cam = plotCamera('Location', [10 0 20], 'Orientation', R, 'Opacity', 0);
% Set view properties
grid on
axis equal
axis manual
% Make the space large enough for the animation.
xlim([-15, 20]);
ylim([-15, 20]);
zlim([15, 25]);
% Make the camera fly in a circle
for theta = 0:pi/64:10*pi
% Rotation about cameras y-axis
T = [cos(theta) 0 sin(theta);
0 1 0;
-sin(theta) 0 cos(theta)];
cam.Orientation = T * R;
cam.Location = [10 * cos(theta), 10 * sin(theta), 20];
drawnow();
end
If you really want to have fun with this, you can supply a function handle to be called when you click on the camera. For example, the function can display the image that the camera sees.

Matlab Aspect Ratio Issue

Recently, I've been using matlab to run a particle aggregation simulation, and I use its plotting itnerface to represent each particle. Thus, I've put together some code which adjusts the size of a marker such that it is equivalent to the actual particle's diameter - in relation to the window's aspect ratio and such. Well, this works very well if I'm dealing with a square domain; see below.
As you can see, the markers are sized such that particles will settle out on topof each other perfectly. Here's the plotting code for that image:
%%Initial Plot at time t = 0 Along with Scaling in the Y-Direction
figure; h=scatter(Pos(1,:),Pos(2,:),6,jet(length(Pos(1,:))),'filled','MarkerEdgeColor','k','linewidth',1);
hold on
axis equal
axis ([0 dp 0 dp]*L*Scale)
currentunits = get(gca,'Units');
set(gca, 'Units', 'Points');
axpos = get(gca,'Position');
set(gca, 'Units', currentunits);
markerWidth = dp/diff(ylim)*axpos(4); % Calculate Marker width in points
set(h, 'SizeData', markerWidth^2)
Well, I added in periodic boundary conditions to my simulation, and this means that I can have 3X the window size shown above without much additional computation cost. So, I wanted to plot up a 3x1 domain (x is basically 3X longer than the y dimension of the domain). However, I'm having troubling sizing the particles appropriately such that they will be scaled properly in the y-direction yet also touch along the x-direction. I think I could do this using some kind of aspect ratio code, but I can't get it to work. Any ideas? Here's the code/result I came up with:
%Plot all of the data
figure;
h=scatter(Pos(1,:),Pos(2,:),6,jet(length(Pos(1,:))),'filled','MarkerEdgeColor','k','linewidth',1);
hold on
axis equal
axis ([0 3*dp 0 3*dp]*L*Scale)
currentunits = get(gca,'Units');
set(gca, 'Units', 'Points');
axpos = get(gca,'Position');
set(gca, 'Units', currentunits);
markerWidth = dp/diff(ylim)*axpos(4); % Calculate Marker width in points
set(h, 'SizeData', (markerWidth)^2)
axis ([0 3*dp 0 dp]*L*Scale)
It produces the following result,which is obviously incorrect :(...
You could use something like
%cirlce def
circx = sin(linspace(0,2*pi,100));
circy = cos(linspace(0,2*pi,100));
%example data
[mgx mgy] = meshgrid(5:5:100, 5:5:100);
x = reshape(mgx,[],1); %circles centers x
y = reshape(mgy,[],1); %circles centers x
r = 2.5*rand(400,1); %radii
c = 250*rand(400,1); %colors
% draw
figure
hold on
for i=1:numel(x)
fill(r(i)*circx+x(i), r(i)*circy+y(i), c(i))
end
so that all all geometric data is fixed into coordinate axes space.
Example output is:

How get position of mouse in my setting axis in matlab?

I have problem with my matlab project.
(my init block)
hFig = figure('Name', 'Form', 'NumberTitle', 'off');
set(hFig, 'PaperUnits', 'points')
axis([0 10 0 10]);
axis square off
when I call this function
MouseCoord = get(src, 'CurrentPoint');
I get mouse's position isn't in my coordinate (different scale axis, I think in picsels)
How get position of mouse in my setting axis [0 10 0 10] ?
Try using a handle to the axis directly:
src = get(hFig,'Children');
MouseCoord = get(src, 'CurrentPoint');
MouseCoord = MouseCoord(1,1:2);
The reason to do with the last line has to do with the different definition of CurrentPoint between figure and axis. Presuming you're only looking at 2D data, you don't need the full output.

Plotting a subplot on top of another plot in Matlab

I need to plot several plots along a sloped line at different positions.
For example, if I:
plot(0:200,'k');
plotpts = 5:5:200;
I would like to be able to plot a smaller plot at each of my plotpts on top of the original 0:200 line.
I know you can use hold on and plot over top that way, but I need to change my origin each time. Does anyone have any suggestions? I would really like to stay in matlab. Thanks!
Here is a flexible way I usually do it:
plot(1:10, 'k')
plotpts = 2:2:8;
mainbox = get(gca, 'Position');
xlims = get(gca, 'XLim');
ylims = get(gca, 'Ylim');
for i=1:length(plotpts)
originx = mainbox(1) + (plotpts(i) - xlims(1)) * (mainbox(3)) / (xlims(2) - xlims(1));
originy = mainbox(2) + (plotpts(i) - ylims(1)) * (mainbox(4)) / (ylims(2) - ylims(1));
axes('position', [originx originy 0.1 0.1], 'Color', 'none')
% Do some plotting here...
end
It's quite a bit of work, but you probably want to use the axes command. A figure window can host any number of axes, where each axes has it's own position, data, annotations, color etc.
The most difficult thing for the application you describe is that each axis position needs to be defined in the coordinate frame of the underlying figure, which means that some math may be required to create the illusion that the axis is correctly positioned within a parent axes/
For example, if you first create a simple plot
figure(1234); clf;
plot(1:10, rand(1,10),'.k-','linewidth',5);
xlim([1 10]);
ylim([0 1]);
set(gca,'color','y'); %This just helps demonstrate the next steps
You can place another axis directly on top of the first, and then
ha = axes('position',[.2 .3 .1 .1])
plot(linspace(0,2*pi,100), sin(linspace(0,2*pi,100)), 'b-')
xlim([0 2*pi])
You can adjust the the properties of the inset axis to suit your particular needs, for example
set(ha,'color','none'); %A transparent axis
set(ha,'xtick',[],'ytick',[]); %Remove tick labels
title(ha,'This is an inset plot')
Is the command subplot not what you're looking for?