フィルターのクリア

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 ?
Struggling in MATLAB
Struggling in MATLAB 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 件のコメント
Struggling in MATLAB
Struggling in MATLAB 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?
Struggling in MATLAB
Struggling in MATLAB 2022 年 6 月 14 日
Nevermind, I can use @David Hill 's response for the workaround. Thank you!

サインインしてコメントする。

その他の回答 (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')});
  2 件のコメント
Struggling in MATLAB
Struggling in MATLAB 2022 年 6 月 14 日
Thanks for looking at the data and your insight. I had no idea that there was dulicate data. This solution works for me.
Struggling in MATLAB
Struggling in MATLAB 2022 年 6 月 14 日
Again, sorry I couldn't accept both answers. :(

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by