Error when trying to plot multiple box plot's in the same graph.

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

We can't reproduce the error with the information you have provided.
I would recommend the following:
Type
dbstop if error
in the command line before you run your code. Then run as normal. The code will stop when the error occurs. You can then examine the workspace to see exactly what it looks like. Presumably, you'll see exactly what is causing the error in boxplot.
Type
dbclear if error
to removing the automatic breakpoint on error, when you are done debugging.
See this documentation for details.
Alright, I did that and it gives the usual error of 'G must be the same length as X or the same length as the number of columns in X.' In the workspace the x is a 6X13 double and the g is a 6X13 double. It also says the xCols is 13 and the gCols is 13. So, I still don't see what it has a problem with as the G and X are the same length and also have the same number of columns?
the cyclist
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.
I just tried to however even when compressed to a zip file the saved workspace was 9,247,500 KB and exceeding the limit for attachments. However, I did find a way to get it to work. Instead of using the boxplot(x,g) syntax, I just did:
boxplot([KncClose KncFar KpSub KSub KlEC KmEC]);
set(gca,'XTickLabel',{'ncClose','ncFar','pSub','Sub','lEC','mEC'})
That seemed to do the same thing, though probably isn't quite the proper way to go about it.

Iniciar sesión para comentar.

 Respuesta aceptada

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 --

Más respuestas (0)

Preguntada:

el 21 de Jun. de 2020

Respondida:

dpb
el 21 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by