video of plot iterations in MATLAB

10 views (last 30 days)
ali hassan
ali hassan on 11 Feb 2021
Edited: KSSV on 11 Feb 2021
i am making a 3d plot using plot3 and i am also using quiver3 in it. i am running 100 iterations and pot keeps on updating. i am able to save the picture of the plot but how can i save or make video of the plot running iterations from matlab?
the picture after 100 iterations is as below:

Answers (1)

KSSV
KSSV on 11 Feb 2021
Edited: KSSV on 11 Feb 2021
Proceed something like shown below:
h = figure;
axis tight manual % this ensures that getframe() returns a consistent size
filename = 'testAnimated.gif';
for n = 1:0.1:10
% Draw plot for y = x.^n
x = 0:0.01:1;
y = 0:0.01:1;
z = x.^n;
plot3(x,y,z)
drawnow
% Capture the plot as an image
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if n == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
  1 Comment
ali hassan
ali hassan on 11 Feb 2021
Edited: KSSV on 11 Feb 2021
I dont know how to input it in my code
CODE:
%TDOA Localization
%3D localization using 04 receivers
clear
clc
close
%% Receivers coordinates
iter = 0;
lat_tgt = 34.0151; long_tgt = 71.5249; z_s = 0.01/375; %%initial coordinates for loop
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; %to bring a change betwen -0.05 and 0.05 in lat_tgt and long_tgt
lat_tgt = lat_tgt+lat_rand; long_tgt = long_tgt+long_rand;
lats = [34.1989 34.0105 34.067894 34.1166 lat_tgt]; %lats=[lats_1 lats_2 lats_3 lats_p lats_s]
longs = [72.0231 71.9876 71.992783 72.0216 long_tgt]; %longs=[longs_1 longs_2 longs_3 longs_p longs_s]
[x,y] = grn2eqa(lats,longs,[34.1166, 72.0216]); %%converting to x,y coordinates with processing unit as a reference.
z=[0 0 0 0]; %%z=[z_1 z_2 z_3 z_p]
c=2.997924580*10^8; %%speed of light
%% Source TDOA calculation
z_s=abs(z_s+(-0.5 + (0.5+0.5)*rand)/37500); %to bring a change betwen -0.05 and 0.05 in z_s(source)
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; %TDOA for receiver 1 and receiver 4(processing unit)
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; %TDOA for receiver 2 and receiver 4(processing unit)
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; %TDOA for receiver 3 and receiver 4(processing unit)
%% Source localization
syms xs ys zs %our unknowns(Symbolic variables)
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; %equation for receiver 1 and receiver 4(processing unit)
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; %equation for receiver 2 and receiver 4(processing unit)
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; %equation for receiver 3 and receiver 4(processing unit)
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); %loop will be executed for the number of solution values of xs
possibleSol(1,m) = double(sol.xs(n)); %solution of xs will be placed in possibleSol(starting from 1st row and 1st column to 1st row and n column)
possibleSol(2,m) = double(sol.ys(n)); %solution of ys will be placed in possibleSol(starting from 2nd row and 1st column to 2nd row and n column)
possibleSol(3,m) = double(sol.zs(n)); %solution of zs will be placed in possibleSol(starting from 3rd row and 1st column to 3rd row and n column)
m=m+1;
end
%%Filtering Results
%As we will get 2 possible solutions for x,y and z so will get one set
%of values in the earth which need to be filtered.
idx = possibleSol(3,:) < 0 | any(imag(possibleSol) ~=0); %an array is defined for which negative solution of z or any imaginary solution will be placed)
possibleSol(:, idx) = []; % colon means all.(:,5) means all rows in column 5.here idx is a column so all rows in idx column will be null
[lat,long] = eqa2grn(possibleSol(1),possibleSol(2),[34.1166, 72.0216]); %now the possible solution is again changed to lat,long from cartesian coordinates.
%Plotting on 3D coordinates
posSolMoving(:,iter) = possibleSol; %a vector(posSolMoving) is made and it contains all possibleSol which has no of colums equal to no of iterations(100)
x_s=x(length(x)); %true value of source in x axis=length of an array which is 5 and it is defined source value
y_s=y(length(y)); %true value of source in y axis=length of an array which is 5 and it is defined source value
x = x(1:length(x)-1); %true value of receivers in x axis=length of an array which is 5 (1:4) and it is defined receivers value
y = y(1:length(y)-1); %true value of receivers in y axis=length of an array which is 5(1:4) and it is defined receivers value
figure(2)
hold on %%previous plot will be retained for next plot to overshadow
grid on
view(3);
plot3(x,y,z, 'ro', 'LineWidth', 2, 'MarkerSize', 10); %it will plot all receivers in red with circle and size of 10 in 3D coordinates
plot3(posSolMoving(1,:),posSolMoving(2,:),posSolMoving(3,:), 'b+', 'LineWidth', 4, 'MarkerSize', 4) %it will plot all solutions in 100 iterations in blue with plus and size of 4 in 3D coordinates
plot3(x_s,y_s,z_s,'g+', 'LineWidth', 1, 'MarkerSize', 4) %it will plot only latest true value of source in green with plus and size of 1 in 3D coordinates
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; %quiver3 plots an arrow matching two points to show relation
end
legend({'Receivers', 'Source','exactvalue'})
view(-14.3,33.2)
pause(0.25)
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!