Error when trying to plot multiple box plot's in the same graph.
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mark Gauthier-Braham
el 21 de Jun. de 2020
Respondida: dpb
el 21 de Jun. de 2020
I'm trying to make a graph that will have 6 different box and whisker plots in it, with the X axis showing the brain area and the Y axis being the time of the first seizure (I'm doing electrophysiology). I want it to look similar to "Create Box Plots for Grouped Data" found here https://uk.mathworks.com/help/stats/boxplot.html#d120e117421. However, when it reaches the boxplot function it gives me the error ""G must be the same length as X or the same length as the number of columns in X." However, I stepped through my script and checked the variables and the X and G are the exact same length (6X13 double), so I'm not sure why it's giving me this error? My script is bellow:
File = input('What file do you want analyze?\n');
variableName = input('What is the name of the variable?\n');
varName = eval(variableName);
cnt = length(varName);
kcnt1Table = zeros(6,cnt);
for i = 1:cnt
colNoClose = input('Which NC is closest?\n');
colNoFar = input('Which NC is farthest?\n');
if ~isempty(varName{1,i}.Savedresults{1,colNoClose}.output.SeizureTimes(:,1))
time1 = varName{1,i}.Savedresults{1,colNoClose}.output.SeizureTimes(1,1);
else
time1 = 0;
end
if ~isempty(varName{1,i}.Savedresults{1,colNoFar}.output.SeizureTimes(:,1))
time2 = varName{1,i}.Savedresults{1,colNoFar}.output.SeizureTimes(1,1);
else
time2 = 0;
end
if ~isempty(varName{1,i}.Savedresults{1,3}.output.SeizureTimes(:,1))
time3 = varName{1,i}.Savedresults{1,3}.output.SeizureTimes(1,1);
else
time3 = 0;
end
if ~isempty(varName{1,i}.Savedresults{1,4}.output.SeizureTimes(:,1))
time4 = varName{1,i}.Savedresults{1,4}.output.SeizureTimes(1,1);
else
time4 = 0;
end
if ~isempty(varName{1,i}.Savedresults{1,5}.output.SeizureTimes(:,1))
time5 = varName{1,i}.Savedresults{1,5}.output.SeizureTimes(1,1);
else
time5 = 0;
end
if ~isempty(varName{1,i}.Savedresults{1,6}.output.SeizureTimes(:,1))
time6 = varName{1,i}.Savedresults{1,6}.output.SeizureTimes(1,1);
else
time6 = 0;
end
kcnt1Table (:,i) = [time1;time2;time3;time4;time5;time6];
end
%% Plotting
ncClose = kcnt1Table(1,:);
ncFar = kcnt1Table(2,:);
pSub = kcnt1Table(3,:);
Sub = kcnt1Table(4,:);
lEC = kcnt1Table(5,:);
mEC = kcnt1Table(6,:);
group = [ ones(size(ncClose));
2* ones(size(ncFar));
3* ones(size(pSub));
4* ones(size(Sub));
5* ones(size(lEC));
6* ones(size(mEC))];
figure(1)
boxplot(kcnt1Table,group)
set(gca,'XTickLabel',{'NC(close)','NC(Far)','pSub','Sub','lEC','mEC'})
title ('Time to First Seizure')
xlabel('Brain Area')
ylabel('Time of First Seizure (s) ')
By the way, I know that eval should be used sparingly and should be avoided, however it was the only function that I could get to work.
4 comentarios
the cyclist
el 21 de Jun. de 2020
Editada: the cyclist
el 21 de Jun. de 2020
Strange. Can you save the workspace as it is at the moment to a MAT file, and upload it here? Use the paper clip icon in the INSERT section of the toolbar that appears in the editing window here.
Respuesta aceptada
dpb
el 21 de Jun. de 2020
Your orientation for kcnt1Table is wrong -- as is you'll have 13 boxes, not six; boxplot treats each column as variable.
You don't need a grouping variable for this case; you've already separated them by the organization by column.
You would/could use a grouping variable in lieu of the 2D array you created the observations were in one vector.
Your code would run as
boxplot(kcnt1Table.',g)
but give an unexpected result -- try it to see...then reread the grouping variable description and the example...altho the error message could perhaps be a little more descriptive of the actual problem --
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!