How to display values from a chart into the command window
14 views (last 30 days)
Show older comments
I am modeling a 3d projectile. I was wondering how to get the end coordinates of the plot to appear in the command window so I dont have to manually find them. Here is the code. I want the values of x,y,z.
figure('name','Projectile Motion')
xlabel('X (m)')
ylabel('Y (m)')
zlabel('Z (m)')
% This sets the viewing angle
view([1 -1 1])
% This sets the limits on the x- and y-axes
xlim([0,3000])
ylim([0,3000])
zlim([0,1000])
box('on')
grid('on')
hold on
% az is azimuth angle, el is elevation
V0 = 150;
az = 45;
el= 30;
t = [0:0.01:25]';
v = sqrt(vx0^2+vy0^2+vz0^2)
vx0 = V0*cosd(az)*cosd(el);
vy0 = V0*sind(az)*cosd(el);
vz0 = V0*sind(el);
x = vx0.*t;
y = vy0.*t;
z = vz0*t-0.5*9.81.*t.^2;
plot3(x,y,z)
%Time of flight
T =(2*vz0)/9.81
2 Comments
Daniel M
on 28 Oct 2019
You have the values already. Remove the semi colon to see them in the command window.
Answers (1)
Daniel M
on 28 Oct 2019
Edited: Daniel M
on 28 Oct 2019
If you want the end value, then do
coord = [x(end) y(end) z(end)]
If you want the value when z = 0, assuming it equals exactly zero,
ind = find(z==0,1,'last');
coord = [x(ind) y(ind) z(ind)]
If you want the closest data point to zero,
[~,ind] = min(abs(z));
If you want to choose the point graphically, select the data tip in the plot, right click and select export cursor to workspace.
3 Comments
Daniel M
on 28 Oct 2019
Method 1 didn't work because you computed your answer for too long and z was negative at the end. Method 2 didn't work because you're not computed z at a timepoint when it equals exactly zero (except at the beginning). And method 3 didn't work because z = 0 at the beginning.
Daniel M
on 28 Oct 2019
Edited: Daniel M
on 28 Oct 2019
There's a zillion ways to find x,y,z when z = 0 at the end of the trajectory.
If z(end) is positive, then increase t and keep computing, or use interp1 with the 'extrapolate' option.
If z(end) is negative, you can use
find(diff(sign(z)))
to find the locations of the zero crossings. From there you can also choose to interpolate the values to get the values when z=0.
Or you can use method 3 from above, but only after the trajectory has peaked.
[~,locmax] = max(z);
[closestToZero,locmin_tmp] = min(abs(z(locmax+1:end)));
locmin = locmax + locmin_tmp;
Or you can use your physics knowledge to solve for t when z = 0 and compute the solution at that time.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!