plotting in matlab using while loop

11 views (last 30 days)
HELLO EVERYONE! i have a simple question regarding plotting in MATLAB using while loop.plzz help me out. my problem is that when i plot using while loop, my green color is coming first and then it is changing and then my blue color is displayed.i want to know what command is making it happen??
CODE:
clear clc close
%% Receivers coordinates
iter = 0;
lat_tgt = 34.0151;
long_tgt = 71.5249;
z_s = 0.01/375;
while iter < 100
iter=iter+1;
%lat_rand = rand/10;
long_rand = rand/10;
lat_rand = (-0.5 + (0.5+0.5)*rand)/10;
long_rand = (-0.5 + (0.5+0.5)*rand)/10;
lat_tgt = lat_tgt+lat_rand; long_tgt = long_tgt+long_rand;
lats = [34.1989 34.0105 34.067894 34.1166 lat_tgt]; longs = [72.0231 71.9876 71.992783 72.0216 long_tgt];
[x,y] = grn2eqa(lats,longs,[34.1166, 72.0216]);
% x=[8 0 -8 0 ].*100; %%x=[x_1 x_2 x_3 x_p]
%x(5) = -x(5); y(5) = -y(5);
% y=[-4 8 -4 5].*100; %%y=[y_1 y_2 y_3 y_p]
z=[0 0 0 0]; %%z=[z_1 z_2 z_3 z_p]
c=2.997924580*10^8;
%%Source TDOA calculation
z_s=z_s+(-0.5 + (0.5+0.5)*rand)/37500;
t1 = (sqrt((x(5)-x(4))^2+(y(5)-y(4))^2+(z_s-z(4))^2)-sqrt((x(5)-x(1))^2+(y(5)-y(1))^2+(z_s-z(1))^2))/c;
t2 = (sqrt((x(5)-x(4))^2+(y(5)-y(4))^2+(z_s-z(4))^2)-sqrt((x(5)-x(2))^2+(y(5)-y(2))^2+(z_s-z(2))^2))/c;
t3 = (sqrt((x(5)-x(4))^2+(y(5)-y(4))^2+(z_s-z(4))^2)-sqrt((x(5)-x(3))^2+(y(5)-y(3))^2+(z_s-z(3))^2))/c;
%%Source localization
syms xs ys zs %our unknowns
eqn1 = sqrt((xs-x(4))^2+(ys-y(4))^2+(zs-z(4))^2)-sqrt((xs-x(1))^2+(ys-y(1))^2+(zs-z(1))^2)-(c*t1)==0;
eqn2 = sqrt((xs-x(4))^2+(ys-y(4))^2+(zs-z(4))^2)-sqrt((xs-x(2))^2+(ys-y(2))^2+(zs-z(2))^2)-(c*t2)==0;
eqn3 = sqrt((xs-x(4))^2+(ys-y(4))^2+(zs-z(4))^2)-sqrt((xs-x(3))^2+(ys-y(3))^2+(zs-z(3))^2)-(c*t3)==0;
sol = solve([eqn1, eqn2, eqn3], [xs ys zs]);
%%Debug
% figure(1)
% fimplicit3(eqn1,[-0.008 0.001 -0.0020 0.002 -0.01 0.02],'FaceAlpha',0.5,'LineStyle','none')
% hold on
% fimplicit3(eqn2,[-0.008 0.001 -0.0020 0.002 -0.01 0.02],'FaceAlpha',0.5,'LineStyle','none')
% hold on
% fimplicit3(eqn3,[-0.008 0.001 -0.0020 0.002 -0.01 0.02],'FaceAlpha',0.5,'LineStyle','none')
%%Debug End
m = 1;
for n = 1:length(sol.xs)
possibleSol(1,m) = double(sol.xs(n));
possibleSol(2,m) = double(sol.ys(n));
possibleSol(3,m) = double(sol.zs(n));
m=m+1;
end
%%Filtering Results
%idx = any(possibleSol < 0,1) | any(imag(possibleSol) ~=0) %possibleSol(3,:) < 0 | any(imag(possibleSol) ~=0)
idx = possibleSol(3,:) < 0 | any(imag(possibleSol) ~=0);
possibleSol(:, idx) = [];
[lat,long] = eqa2grn(possibleSol(1),possibleSol(2),[34.1166, 72.0216]);
%Plotting on 3D coordinates
% fprintf('Xs=%g\n', possibleSol(1,1));
% fprintf('Ys=%g\n', possibleSol(2,1));
% fprintf('Zs=%g\n', possibleSol(3,1));
% scatter3(x, y, z, 'o');
% hold on
% scatter3(possibleSol(1,1), possibleSol(2,1), possibleSol(3,1), '+');
% hold off
% legend({'Receivers', 'Source'})
posSolMoving(:,iter) = possibleSol;
x_s=x(length(x)); y_s=y(length(y)); x = x(1:length(x)-1); y = y(1:length(y)-1);
%fprintf('Xs=%.f\nYs=%.f\nZs=%.f\n', possibleSol);
figure(1)
hold on
grid on
view(3);
plot3(x,y,z, 'ro', 'LineWidth', 2, 'MarkerSize', 10);
plot3(posSolMoving(1,:),posSolMoving(2,:),posSolMoving(3,:), 'b+', 'LineWidth', 4, 'MarkerSize', 4)
plot3(x_s,y_s,z_s,'g+', 'LineWidth', 2, 'MarkerSize', 4)
if iter > 1
q = quiver3(posSolMoving(1,iter-1),posSolMoving(2,iter-1),posSolMoving(3,iter-1),posSolMoving(1,iter)-posSolMoving(1,iter-1),posSolMoving(2,iter)-posSolMoving(2,iter-1),posSolMoving(3,iter)-posSolMoving(3,iter-1),0);
q.LineWidth=0.75;
end
legend({'Receivers', 'Source','exactvalue'})
view(-14.3,33.2)
pause(1)
end
output:

Accepted Answer

per isakson
per isakson on 28 Nov 2020
Edited: per isakson on 28 Nov 2020
The answer ought to be, step throw the code and observe what each line of code makes to your diagram. However, the problem is that the figure window constantly disappears under other windows. There are to ways to keep the figure window visible.
I've modified your code somewhat to better see what's going on.
fh = figure(1);
WindowAPI( fh,'Topmost') % <<<<<<<<<<<<<<<<<
hold on
grid on
view(3);
plot3(x,y,z, 'ro', 'LineWidth', 2, 'MarkerSize', 10);
plot3(posSolMoving(1,:),posSolMoving(2,:),posSolMoving(3,:), 'b+' ...
, 'LineWidth', 2, 'MarkerSize', 6)
plot3(x_s,y_s,z_s,'go', 'LineWidth', 2, 'MarkerSize', 8)
In the first iteration (iter==1)
  • The first plot3 draws four red circles
  • The second plot3 draws a blue cross and changes the axis limits.
  • The third plot3 adds (in my case) a green circle on top of the blue cross with . "my green color is coming first and then it is changing" no it's hidden by the green marker that is added on top.
Now it's your turn to step throw the remaining 99 iterations.
  2 Comments
ali hassan
ali hassan on 28 Nov 2020
Edited: per isakson on 29 Nov 2020
THANKS SIR FOR YOUR SINCERE NEEDED HELP.
Just need a bit of more help. As you saw that there were 100 arrows which were made using quier3. Actually I want that when the latest arrow appears, previous arrows should dissappear.
Kindly help in this regard,
BEST REGARDS.
per isakson
per isakson on 29 Nov 2020
Edited: per isakson on 29 Nov 2020
"Just need a bit of more help" I fear you'll need more than a bit.
"latest arrow appears, previous arrows should dissappear" What about the markers at the end of the arrows? Should the previous markers also dissapear?
Step throw the code to actually see what's going on.
As an illustration you can fit this into your code. It does sort of what you ask for.
[~] = plot3(x,y,z, 'ro', 'LineWidth', 2, 'MarkerSize', 10);
lhb = plot3(posSolMoving(1,:),posSolMoving(2,:),posSolMoving(3,:), 'b+' ...
, 'LineWidth', 2, 'MarkerSize', 6);
lhg = plot3(x_s,y_s,z_s,'go', 'LineWidth', 2, 'MarkerSize', 8);
if iter > 1
if iter >= 3
delete( q )
end
q = quiver3( posSolMoving(1,iter-1) ...
,posSolMoving(2,iter-1) ...
,posSolMoving(3,iter-1) ...
,posSolMoving(1,iter)-posSolMoving(1,iter-1) ...
,posSolMoving(2,iter)-posSolMoving(2,iter-1) ...
,posSolMoving(3,iter)-posSolMoving(3,iter-1) ...
,0 ); % 0 to plot the vectors without the automatic scaling.
q.LineWidth=2;
end
legend({'Receivers', 'Source','exactvalue'})
pause(.5)
delete( lhb )
delete( lhg )
IMO: It's difficult to make an animation work well with hold('on') and plot3(). When I change one thing some smart default value introduce an unwanted side effect somewhere else. It's like playing whack-a-mole.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!