Why am I getting error "Vectors must be the same length" although they are of same length?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Torsten
le 13 Juin 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 ?
Réponse acceptée
Voss
le 13 Juin 2022
Modifié(e) : Voss
le 13 Juin 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{:});
Plus de réponses (1)
David Hill
le 13 Juin 2022
Modifié(e) : David Hill
le 13 Juin 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')});
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!