How do I plot both of these different sized arrays on the same plot?

So, I am trying to do 1-dim projectile motion for a ping pong ball and a golf ball with drag. I need to plot both of the velocities on one graph, and both of the change in positon (Y) in another. everything else with my code runs smoothly, but when I go to plot them both over time, I get the error:
'Error using plot'
'Vectors must be the same length.'
My vectors are as follows:
%Vpp is a 1x2442 matrix
%Ypp is a 1x2442 matrix
%Tpp is a 1x2442 matrix
%Vgb is a 1x4256 matrix
%Ygb is a 1x4256 matrix
%Tgb is a 1x4256 matrix
My code for plotting is as follows:
subplot(2,1,1)
plot (Tgb,Ygb)
hold on
plot (Tgb,Ypp)
subplot(2,1,2)
plot (Tpp,Vgb)
hold on
plot (Tpp,Vpp)
How do I need to change the code so that I can plot these both on 2 seperate graphs?

댓글 수: 2

If I am not wrong, you are getting this error at this line
plot (Tgb,Ypp)
Because you are trying to plot 4256 elements on x-axis and 2442 elements on y-axis which is ofcourse meaningless.
Can you try to draw the expected output on paint and attach a image here?
This helped a lot. I was a dumb dumb and did not realize I was using the time variable with the wrong subscripts Thanks!

댓글을 달려면 로그인하십시오.

 채택된 답변

Rik
Rik 2020년 4월 12일
Assuming your x-axis is time and Tgb and Tpp encode that:
subplot(2,1,1)
plot (Tgb,Ygb)
hold on
plot (Tpp,Ypp)
hold off
subplot(2,1,2)
plot (Tgb,Vgb)
hold on
plot (Tpp,Vpp)
hold off
The point is that you should mix different lengths arrays, just like the error is telling you.

댓글 수: 1

This helped a lot. I was a dumb dumb and did not realize I was using the time variable with the wrong subscripts Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2020년 4월 12일
...
plot (Tgb,Ygb)
...
plot (Tgb,Ypp)
plot (Tpp,Vgb)
...
plot (Tpp,Vpp)
For some reason, you're mixing metaphors here by using what one presumes is time vector for one with position, velocity for the other...
subplot(2,1,1)
title('Position')
plot(Tgb,Ygb,'b-',Tpp,Ypp,'r-')
legend('Golf ball','Ping Pong ball')
subplot(2,1,2)
title('Velocity')
plot(Tgb,Vgb,'b-',Tpp,Vpp,'r-')
legend('Golf ball','Ping Pong ball')
Salt to suit...

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

질문:

2020년 4월 12일

댓글:

2020년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by