dynamic Polar plot on normal plot - matlab

Request a big help on this. I am trying to use a dynamic polar plot on normal plot. I have a random waypoint model. Now, I would like to put a dynamically changing polar plot to reach a few selected users in the random waypoint.
function test_Animate(s_mobility,s_input,time_step)
v_t = 0:time_step:s_input.SIMULATION_TIME;
for nodeIndex = 1:s_mobility.NB_NODES
%Simple interpolation (linear) to get the position, anytime.
%Remember that "interp1" is the matlab function to use in order to
%get nodes' position at any continuous time.
vs_node(nodeIndex).v_x = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_X,v_t);
vs_node(nodeIndex).v_y = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_Y,v_t);
end
fig = figure;
hold on;
for nodeIndex = 1:s_mobility.NB_NODES
vh_node_pos(nodeIndex) = plot(vs_node(nodeIndex).v_x(1),vs_node(nodeIndex).v_y(1),'*','color',[0.3 0.3 1]);
axis off;
end
title(cat(2,'Simulation time (sec): ',num2str(s_mobility.SIMULATION_TIME)));
xlabel('X (meters)');
ylabel('Y (meters)');
title('Random Waypoint mobility');
ht = text(min(vs_node(1).v_x),max(vs_node(1).v_y),cat(2,'Time (sec) = 0'));
axis([min(vs_node(1).v_x) max(vs_node(1).v_x) min(vs_node(1).v_y) max(vs_node(1).v_y)]);
disp(min(vs_node(1).v_x));
disp(max(vs_node(1).v_x));
disp(min(vs_node(1).v_y));
disp(max(vs_node(1).v_y));
hold off;
for timeIndex = 1:length(v_t);
t = v_t(timeIndex);
set(ht,'String',cat(2,'Time (sec) = ',num2str(t,4)));
for nodeIndex = 1:s_mobility.NB_NODES
set(vh_node_pos(nodeIndex),'XData',vs_node(nodeIndex).v_x(timeIndex),'YData',vs_node(nodeInd ex).v_y(timeIndex));
curr_x_pos=vs_node(nodeIndex).v_x(timeIndex);
% curr_y_pos=vs_node(nodeIndex).v_y(timeIndex);
%disp(curr_x_pos);
%curr_y_pos=vs_node(nodeIndex).v_y(timeIndex);
end
drawnow;
end
end
Now, in this above code, I need to add a polar plot which has directional antenna beam. Please help me out as to how to change the beam position according to user position which we can see on the plot. As in, the beam should encircle the user position dynamically. The other files regarding this are present code are in the following link. RANDOM WAYPOINT MOBILITY MODEL. Request a big help.
Thanks in advance.

Related

How to draw a line at a bearing angle in matlab?

I'm trying to simulate the movement of a target in Matlab, whose initial x and y co-ordinates, true bearing and speed (in m/s) are specified. I am wondering if there is a way to simply draw a straight line, at the specified bearing angle to show the path taken by the target (as shown in the image below)
Thanks in advance!
Your best bet is to rely on one of the built-in polar plotting functions to do this. I think the one that is most similar to your needs would be compass. It essentially plots an arrow pointing from the center to a point (defined in cartesian coordinates) on a polar plot.
theta = deg2rad(130);
% Your speed in m/s
speed = 5;
hax = axes();
c = compass(hax, speed * cos(theta), speed * sin(theta));
% Change the view to orient the axes the way you've drawn
view([90 -90])
Then in order to change the bearing and speed, you simply call the compass function again with your new bearing/speed.
new_theta = deg2rad(new_angle_degrees);
c = compass(hax, new_speed * cos(new_theta), new_speed * sin(new_theta));
Other polar plotting options include polar and polarplot which accept polar coordinates but don't have an arrow head. If you don't like the polar plot you could always go with quiver on a cartesian axes (making sure you specify the same axes).
Edit
Based on your feedback and request, below is an example of a polar plot of the distance traveled.
% Speed in m/s
speed = 5;
% Time in seconds
time = 1.5;
% Bearing in degrees
theta = 130;
hax = axes();
% Specify polar line from origin (0,0) to target position (bearing, distance)
hpolar = polar(hax, [0 deg2rad(theta)], [0 speed * time], '-o');
% Ensure the axis looks as you mentioned in your question
view([90 -90]);
Now to update this plot with a new bearing, speed, time you would simply call polar again specifying the axes.
hpolar = polar(hax, [0 theta], [0 speed], '-o');
I am not sure if I got it correctly, here is my solution:
figure;hold on; % Create figure
x_start = 10;% Starting position
y_start = 20;
plot(x_start+[-1 1],[y_start y_start],'k');% Plot crosshairs
plot([x_start x_start],y_start+[-1 1],'k');
angle = -(130-90)*pi/180; % Bearing angle 130° like in your graph
x_target = x_start+10*cos(angle); % Calculation of target position
y_target = y_start+10*sin(angle);
plot(x_target+[-1 1],[y_target y_target],'k');% Plot crosshairs
plot([x_target x_target],y_target+[-1 1],'k');
% Draw line between start and target
plot([x_start x_target],[y_start y_target],'g');
set(gca,'xlim',[0 30],'ylim',[0 30]); % Adjust axes
text(x_start+1,y_start,'Start'); % Write text to points
text(x_target+1,y_target,'End');

How to animate points on an image in MATLAB?

I have the pixel locations of P points on a -constant- image, for T iterations of an algorithm, so locations = [T x 2*P] double.
Now I want to create an animation where it plots the image, then plots the points, pauses for N seconds and updates their location to the next step. I don't know if there is a standard way to follow. I think I need something like:
figure;
imshow(img);
hold on;
for t=1:T
anim = updatePlot(locations(t,:), anim); % ?
end
How can I implement this updatePlot function?
Thanks for any help!
You can do this a couple of different ways. The first way would be to give the plotted points a handle so that you can delete them before the next iteration:
figure
imshow(img);
hold on;
for t = 1:T
% delete the previous points plotted (skip t = 1 since they won't exist)
if t > 1
delete(hPoints);
end
hPoints = plot(xLocations(t,:),yLocations(t,:),'.');
getframe;
pause(N);
end
(I am not exactly sure how you parse your locations along each row to separate the x and y components, so I've just used xLocations and yLocations to represent those values.)
The second way would be to re-draw the entire image at each iteration:
figure
for t = 1:T
clf;
imshow(img);
hold on;
plot(xLocations(t,:),yLocations(t,:),'.');
getframe;
pause(N);
end
Note that imshow might have its own getframe effect so that you'll see the image flicker before plotting the points -- if that happens just switch from imshow to image.

editing the random waypoint mobility matlab for a constant node

Please help me out with this. This plot gives the mobile users that are moving. Now, I am trying to add a few constant points in the plot. Like the base stations for the mobile users '*'. I want to put say a '#' for every base station and put like around 10 of them.
function test_Animate(s_mobility,s_input,time_step)
v_t = 0:time_step:s_input.SIMULATION_TIME;
for nodeIndex = 1:s_mobility.NB_NODES
%Simple interpolation (linear) to get the position, anytime.
%Remember that "interp1" is the matlab function to use in order to
%get nodes' position at any continuous time.
vs_node(nodeIndex).v_x = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_X,v_t);
vs_node(nodeIndex).v_y = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_Y,v_t);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;
hold on;
for nodeIndex = 1:s_mobility.NB_NODES
vh_node_pos(nodeIndex) = plot(vs_node(nodeIndex).v_x(1),vs_node(nodeIndex).v_y(1),'*','color',[0.3 0.3 1]);
end
title(cat(2,'Simulation time (sec): ',num2str(s_mobility.SIMULATION_TIME)));
xlabel('X (meters)');
ylabel('Y (meters)');
title('Random Waypoint mobility');
ht = text(min(vs_node(1).v_x),max(vs_node(1).v_y),cat(2,'Time (sec) = 0'));
axis([min(vs_node(1).v_x) max(vs_node(1).v_x) min(vs_node(1).v_y) max(vs_node(1).v_y)]);
hold off;
for timeIndex = 1:length(v_t);
t = v_t(timeIndex);
set(ht,'String',cat(2,'Time (sec) = ',num2str(t,4)));
for nodeIndex = 1:s_mobility.NB_NODES
set(vh_node_pos(nodeIndex),'XData',vs_node(nodeIndex).v_x(timeIndex),'YData',vs_node(nodeIndex).v_y(timeIndex));
end
drawnow;
end
end
The complete submission of this can be got in the MATHworks site.
http://www.mathworks.in/matlabcentral/fileexchange/30939-random-waypoint-mobility-model
Please help me out.
Here's the basic idea. In the following, fixed_x and fixed_y are vectors of equal length giving the location of your fixed points.
moving_x and moving_y are matrices of size t x n where t is the number of time steps, and n the number of points.
%plot the fixed points
plot(fixed_x,fixed_y, 'p');
hold on
%plot the first set of moving points
h = plot(moving_x(1,:),moving_y(1,:),'r*');
Then, to move the points, we use the handle to the moving points, h to set XData and YData values. This would be within a loop where n is the time step:
set(h, 'XData',moving_x(n,:),'YData',moving_y(n,:));
%may need drawnow;
If you want to be able to see the points moving, you may need to add some pause command - otherwise it might cycle through the update loop so quickly you won't see anything except the final set of positions. On the other hand, you could just save out the figure as an image file after you've moved the points with set, or add it as a frame to a movie, etc.

How to limit x-axis of 3D animated mesh in MATLAB

I have an animated 3D mesh of a sine wave, but i want to limit all my axis to specific ranges.
Here is a piece my code:
[x,t] = meshgrid(x,t);
j=1;
while 1
j=j+1;
y = a*sind(2*pi*f*(j/100+(x*T)/wl));
mesh(y);
F(j) = getframe;
end
movie(F);
the values of the variables a,f,T and wl are predefined by the user
i want to limit the x-axis from 10 to 20, can someone show me how to do it?
Call xlim after you draw the mesh.
xlim([10 20])

MATLAB plot moving data points in separate subplots simultaneously

I wish to visualize the movement of a data point throughout space across a period of time within MATLAB. However, the way I want my figure to display is such that only a single instant is plotted at any given time. That was easy, I simply created a for loop to update my 3D plot display for every set of coordinates (x,y,z) in my data. However, I wish to display 4 different viewing angles of this plot at all times. I am well aware of how to setup subplots within MATLAB, that is not the issue. My issue is getting all 4 of these subplots to execute simultaneously so that all 4 subplots are always displaying the same point in time.
How to handle this issue?
As requested, my code for a figure with a single plot is shown below:
datan = DATA; %data in form of x,y,z,a,b,c by column for row# of time points
tib=zeros(size(datan,1),12);
tib(:,1:3) = datan(:,1:3);
tib_ref=tib(1,1:3);
for i=1:size(datan,1)
tib(i,1:3)=tib(i,1:3)-tib_ref;
end
angle_to_dircos
close all
figure('Name','Directions (Individual Cycles)','NumberTitle','off')
for cc=1:2
hold off
for bb=1:10:size(tib,1);
scatter3(tib(bb,1),tib(bb,2),tib(bb,3),'green','filled'); %z and y axes are flipped in polhemus system
hold on
p0 = [tib(bb,1),tib(bb,2),tib(bb,3)];
p1 = [tib(bb,1)+10*tib(bb,4),tib(bb,2)+10*tib(bb,5),tib(bb,3)+10*tib(bb,6)];
p2 = [tib(bb,1)+10*tib(bb,7),tib(bb,2)+10*tib(bb,8),tib(bb,3)+10*tib(bb,9)];
p3 = [-(tib(bb,1)+100*tib(bb,10)),-(tib(bb,2)+100*tib(bb,11)),-(tib(bb,3)+100*tib(bb,12))];
vectarrow(p0,p1,1,0,0)
hold on
vectarrow(p0,p2,0,1,0)
hold on
vectarrow(p0,p3,0,0,1)
hold on
az = 90;
el = 0;
view(az, el);
xlim([-50,50]);
ylim([-50,50]);
zlim([-50,50]);
xlabel('distance from center in X');
ylabel('distance from center in Y');
zlabel('distance from center in Z');
title('XYZ Scatter Plots of Tracker Position');
hold on
plot3(0,0,0,'sk','markerfacecolor',[0,0,0]);
p0 = [0,0,0];
p1 = [10,0,0];
p2 = [0,10,0];
p3 = [0,0,100];
vectarrow(p0,p1,1,0,0)
hold on
vectarrow(p0,p2,0,1,0)
hold on
vectarrow(p0,p3,1,0,1)
drawnow;
end
end
If you use set to update the x and y-data of your points, rather than recreating the plot entirely every time, the update will be simultaneous thanks to Matlab waiting for drawnow.
Here's an example
figure,
subplot(1,2,1),plot(rand(10,1),rand(10,1),'.'),hold on,p1=plot(rand(1),rand(1),'.r')
subplot(1,2,2),plot(rand(10,1),rand(10,1),'.'),hold on,p2=plot(rand(1),rand(1),'.r')
%# read the red coordinates - I should have stored them before plotting :)
x(1) = get(p1,'xdata');y(1)=get(p1,'ydata');x(2)=get(p2,'xdata');y(2)=get(p2,'ydata');
%# animate
for i=1:100,
delta = randn(1,2)*0.01;
x=x+delta(1);
y=y+delta(2);
set(p1,'xdata',x(1),'ydata',y(1));
set(p2,'xdata',x(2),'ydata',y(2));
pause(0.1),
drawnow, %# I put this in case you take out the pause
end