Why am I getting error "Vectors must be the same length" although they are of same length?

1 次查看(过去 30 天)
I am trying to plot a X,Y graph. I am getting an error "Vectors must be the same length", although I see in the workspace that they are of the same length. It is working for most of the trials in the table, but throwing an error for some entries. What could be the problem? I have attached the table. Here is my code for the plot.
plot(subject_data.xcoordinates2{subject_data.trialname == 'Trial40'}, ...
subject_data.ycoordinates2{subject_data.trialname == 'Trial40'});
The error is as follows.
Error using plot
Vectors must be the same length.
Error in maze_outlier (line 137)
plot(subject_data.xcoordinates2{subject_data.trialname == 'Trial40'}, ...
  2 个评论
Torsten
Torsten 2022-6-13
Before the plot command, insert
size(subject_data.xcoordinates2{subject_data.trialname == 'Trial40'})
size(subject_data.ycoordinates2{subject_data.trialname == 'Trial40'})
What do you get as output ?
Atanu
Atanu 2022-6-14
It said too many input arguments. Anyway, I got help to identify the problem. Thanks for your comment.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-6-13
编辑:Voss 2022-6-13
load('subject_data.mat')
disp(subject_data)
subjectid mazenumber feeder trialname date playstarttrialtone presentcost coordinatetimes2 xcoordinates2 ycoordinates2 truextail trueytail truexnose trueynose truedirection _________ __________ ______ _________ ___________ __________________ ___________ ________________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ "renata" maze 4 {[3]} Trial40 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[3]} Trial19 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[4]} Trial8 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[4]} Trial13 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[2]} Trial1 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[2]} Trial5 16-May-2022 {[ 2]} {[2.5000]} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} "renata" maze 4 {[3]} Trial2 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[1]} Trial3 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[3]} Trial4 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[1]} Trial15 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[4]} Trial6 16-May-2022 {[ 2]} {[2.5000]} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} "renata" maze 4 {[1]} Trial7 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[2]} Trial9 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[2]} Trial10 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[3]} Trial14 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[3]} Trial11 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[4]} Trial12 16-May-2022 {[ 2]} {[2.5000]} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} "renata" maze 4 {[1]} Trial16 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[2]} Trial17 16-May-2022 {[ 2]} {[2.5000]} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} "renata" maze 4 {[4]} Trial18 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[4]} Trial20 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[1]} Trial21 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[4]} Trial34 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[2]} Trial22 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[2]} Trial36 16-May-2022 {[ 2]} {[2.5000]} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} "renata" maze 4 {[3]} Trial23 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[2]} Trial24 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[4]} Trial25 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[4]} Trial31 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[1]} Trial26 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[2]} Trial27 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[3]} Trial28 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[3]} Trial29 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[4]} Trial30 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[1]} Trial32 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[1]} Trial33 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[1]} Trial35 16-May-2022 {[ 2]} {[2.5000]} {283×1 double} {283×1 double} {283×1 double} {283×1 double} {283×1 double} {283×1 double} {283×1 double} {283×1 double} "renata" maze 4 {[2]} Trial37 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[3]} Trial38 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[1]} Trial39 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[2]} Trial1 16-May-2022 {[ 2]} {[2.5000]} {301×1 double} {301×1 double} {301×1 double} {301×1 double} {301×1 double} {301×1 double} {301×1 double} {301×1 double} "renata" maze 4 {[3]} Trial2 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[1]} Trial3 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[3]} Trial4 16-May-2022 {[ 2]} {[2.5000]} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} "renata" maze 4 {[2]} Trial5 16-May-2022 {[ 2]} {[2.5000]} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} "renata" maze 4 {[4]} Trial6 16-May-2022 {[NaN]} {[ NaN]} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} {606×1 double} "renata" maze 4 {[1]} Trial7 16-May-2022 {[ 2]} {[2.5000]} {309×1 double} {309×1 double} {309×1 double} {309×1 double} {309×1 double} {309×1 double} {309×1 double} {309×1 double} "renata" maze 4 {[4]} Trial8 16-May-2022 {[ 2]} {[2.5000]} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} "renata" maze 4 {[2]} Trial9 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[2]} Trial10 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[1]} Trial15 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[3]} Trial11 16-May-2022 {[ 2]} {[2.5000]} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} "renata" maze 4 {[4]} Trial12 16-May-2022 {[ 2]} {[2.5000]} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} "renata" maze 4 {[4]} Trial13 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[3]} Trial14 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[1]} Trial16 16-May-2022 {[ 2]} {[2.5000]} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} {290×1 double} "renata" maze 4 {[2]} Trial17 16-May-2022 {[ 2]} {[2.5000]} {282×1 double} {282×1 double} {282×1 double} {282×1 double} {282×1 double} {282×1 double} {282×1 double} {282×1 double} "renata" maze 4 {[4]} Trial18 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[3]} Trial19 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[4]} Trial20 16-May-2022 {[ 2]} {[2.5000]} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} {271×1 double} "renata" maze 4 {[1]} Trial21 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[2]} Trial22 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[3]} Trial23 16-May-2022 {[ 2]} {[2.5000]} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} "renata" maze 4 {[2]} Trial24 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[4]} Trial25 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[1]} Trial26 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[2]} Trial27 16-May-2022 {[ 2]} {[2.5000]} {302×1 double} {302×1 double} {302×1 double} {302×1 double} {302×1 double} {302×1 double} {302×1 double} {302×1 double} "renata" maze 4 {[3]} Trial28 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[3]} Trial29 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[4]} Trial30 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[4]} Trial31 16-May-2022 {[ 2]} {[2.5000]} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} {272×1 double} "renata" maze 4 {[1]} Trial32 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[1]} Trial33 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[4]} Trial34 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} "renata" maze 4 {[1]} Trial35 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[2]} Trial36 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[2]} Trial37 16-May-2022 {[ 2]} {[2.5000]} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} {281×1 double} "renata" maze 4 {[3]} Trial38 16-May-2022 {[ 2]} {[2.5000]} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} {280×1 double} "renata" maze 4 {[1]} Trial39 16-May-2022 {[ 2]} {[2.5000]} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} {291×1 double} "renata" maze 4 {[3]} Trial40 16-May-2022 {[ 2]} {[2.5000]} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double} {270×1 double}
% There are 2 Trial40's in the table:
find(subject_data.trialname == 'Trial40')
ans = 2×1
1 80
% To plot both, you can collect the x- and y-coordinates in a cell array like this:
args = { ...
subject_data.xcoordinates2{subject_data.trialname == 'Trial40'} ...
subject_data.ycoordinates2{subject_data.trialname == 'Trial40'}}
args = 1×4 cell array
{281×1 double} {270×1 double} {281×1 double} {270×1 double}
% but they are in order [x1 x2 y1 y2], so you have to make them [x1 y1 x2 y2]:
args = args([1:2:end 2:2:end])
args = 1×4 cell array
{281×1 double} {281×1 double} {270×1 double} {270×1 double}
% and then send them to plot() in that order:
plot(args{:});
  2 个评论
Atanu
Atanu 2022-6-14
Thanks for looking at the data and your insight. I had no idea that there was dulicate data. However, I believe this occurance is rare. I want to bypass it by just accessing the 1st occurance of 'Trial40'. (I know there will be error, but I have loads of data. So it should not affect much) How do I plot just the first one?

请先登录,再进行评论。

更多回答(1 个)

David Hill
David Hill 2022-6-13
编辑:David Hill 2022-6-13
You have two 'Trial40'
plot(subject_data.xcoordinates2{find(subject_data.trialname == 'Trial40',1)}, ...
subject_data.ycoordinates2{find(subject_data.trialname == 'Trial40',1)});
plot(subject_data.xcoordinates2{find(subject_data.trialname == 'Trial40',1,'last')}, ...
subject_data.ycoordinates2{find(subject_data.trialname == 'Trial40',1,'last')});

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by