Asked by Priya
on 19 Aug 2014

I get this error in the plot line.

V=20;

t_sim=0.02;

sample_distance=0.2;

ts=sample_distance/V;

step_time=1;

step_size=0.01;

collect_ts=ts*1;

t_plot=(0:collect_ts:t_sim);

figure(6);

plot(t_plot,simout(:,1));hold all;plot(x,y);grid on;

xlabel('Time (s)');ylabel('lateral position (m)');

>> size(t_plot)

ans =

1 3

>> size(simout)

ans =

21 71

Can anyone please tell the reason for this error and how to solve it.

Answer by Kyle
on 19 Aug 2014

I tried to reproduce your error, but the variables "simout," "x," and "y" are undefined in your included code.

Vectors must be same lengths suggests your rows and columns are not the same for t_plot and simout. It looks like t_plot has 1 row and 3 columns, while simout has 21 rows and 71 columns.

Kyle
on 20 Aug 2014

Since your simout is 71 columns, t_plot should be too. Are you sure you want to plot t_plot,simout on the same plot as x,y? I ask because the max for t_plot = t_sim = 0.02, while x max is 5. When plotted together you can't clearly see the t_plot,simout (see plots below). The following works as an example:

simout = 0.01*rand(1,71);

d = 0.01; x=[0,1,1,5]; y=[0,0,d,d];

t_sim=0.02;

sample_distance=0.2;

V=70; %changed

ts=t_sim/V; %changed

step_time=1;

step_size=0.01;

collect_ts=ts*1;

t_plot=(0:collect_ts:t_sim); %collect_ts (and ts) must equal t_sim/(71-1)

figure(6);

plot(t_plot,simout); grid on; hold all; plot(x,y);

xlabel('Time (s)');ylabel('lateral position (m)');

Although I didn't use the sample_distance variable at all.

Priya
on 20 Aug 2014

This has given me some clue. Anyway thanks for your help.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Adam (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/151567-error-using-plot-vectors-must-be-the-same-lengths#comment_232490

## Priya (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/151567-error-using-plot-vectors-must-be-the-same-lengths#comment_232502

## Adam (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/151567-error-using-plot-vectors-must-be-the-same-lengths#comment_232503

## Priya (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/151567-error-using-plot-vectors-must-be-the-same-lengths#comment_232506

Sign in to comment.