Why am I getting error "Vectors must be the same length" although they are of same length?
1 view (last 30 days)
Show older comments
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 Comments
Torsten
on 13 Jun 2022
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 ?
Accepted Answer
Voss
on 13 Jun 2022
Edited: Voss
on 13 Jun 2022
load('subject_data.mat')
disp(subject_data)
% There are 2 Trial40's in the table:
find(subject_data.trialname == 'Trial40')
% 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'}}
% 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])
% and then send them to plot() in that order:
plot(args{:});
More Answers (1)
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')});
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!