Why am I getting error "Vectors must be the same length" although they are of same length?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Torsten
el 13 de Jun. de 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 ?
Respuesta aceptada
Voss
el 13 de Jun. de 2022
Editada: Voss
el 13 de Jun. de 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{:});
Más respuestas (1)
David Hill
el 13 de Jun. de 2022
Editada: David Hill
el 13 de Jun. de 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')});
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!