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

3 views (last 30 days)
Struggling in MATLAB on 13 Jun 2022
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 CommentsShowHide 1 older comment
Struggling in MATLAB on 14 Jun 2022
It said too many input arguments. Anyway, I got help to identify the problem. Thanks for your comment.

Voss on 13 Jun 2022
Edited: Voss on 13 Jun 2022
disp(subject_data)
subjectid mazenumber feeder trialname date playstarttrialtone presentcost coordinatetimes2 xcoordinates2 ycoordinates2 truextail trueytail truexnose trueynose truedirection _________ __________ ______ _________ ___________ __________________ ___________ ________________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ "renata" maze 4 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 {} 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 CommentsShowHide 1 older comment
Struggling in MATLAB on 14 Jun 2022
Nevermind, I can use @David Hill 's response for the workaround. Thank you!

David Hill on 13 Jun 2022
Edited: David Hill on 13 Jun 2022
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 CommentsShowHide 1 older comment
Struggling in MATLAB on 14 Jun 2022
Again, sorry I couldn't accept both answers. :(

### Categories

Find more on Annotations in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8011675,"math_prob":0.8935501,"size":2091,"snap":"2023-14-2023-23","text_gpt3_token_len":554,"char_repetition_ratio":0.21226642,"word_repetition_ratio":0.041666668,"special_character_ratio":0.29076996,"punctuation_ratio":0.1980198,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99233544,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-21T23:42:44Z\",\"WARC-Record-ID\":\"<urn:uuid:5ef3f2ff-fb0a-44f3-bade-79bf0d5edbcb>\",\"Content-Length\":\"200967\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bbbecbfc-5a9a-4295-b727-101dcacd1916>\",\"WARC-Concurrent-To\":\"<urn:uuid:6db1ab6e-2dbc-46f7-b7fd-5e8e164a2d06>\",\"WARC-IP-Address\":\"23.34.160.82\",\"WARC-Target-URI\":\"https://nl.mathworks.com/matlabcentral/answers/1739700-why-am-i-getting-error-vectors-must-be-the-same-length-although-they-are-of-same-length?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:PEUOLB2K3W4V3PM55JRIQP3DL3JZH53N\",\"WARC-Block-Digest\":\"sha1:2GYUUOPJTDAWA23K2W64Z7NUOP2ORQTU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296943747.51_warc_CC-MAIN-20230321225117-20230322015117-00755.warc.gz\"}"}