MATLAB: Bug in boxplot

boxplotgraphicsMATLABstatisticsStatistics and Machine Learning Toolbox

I get what it seems a weird behavior from boxplot. If I have an array with a single column, and specify groups as the scalar 1, AND specify ANY additional parameters, I get an error (see example below). The error is about parameter 'Orientation' which is not the one specified. However, if I don't specify additional parameters OR if the scalar is anything else (say 2, or a cell) there is no error. Is this a bug? Any suggestions about it?. I understand the syntax may seem weird but I need it for a function to plot nicer boxplots.
y= normrnd(0,1,100,1);
no problem
no problem
Error using boxplot>parseArgs (line 591)
'.' is not a valid value for the 'Orientation' argument. The value must be 'horizontal' or 'vertical'.
Error in boxplot (line 251)
[ax,x,g,notch,symbol,orientation,whisker,labels,labelverbosity, ...
no problem
no problem

Best Answer

Hi Francisco,
I understand the issue you are facing. The BOXPLOT documentation states that: boxplot(X,G) specifies one or more grouping variables G, producing a separate box for each set of X values sharing the same G value or values. Grouping variables must have one row per element of X, or one row per column of X.
There are two possible scenarios for the arguments that BOXPLOT takes:
Scenario 1) First Argument to BOXPLOT function is a vector:
In this case, the second argument also has to be a vector of the same size. So for example:
>> y = randn(100,1); boxplot(y,ones(size(y)),'symbol','.')
Scenario 2) First argument to the BOXPLOT function is a matrix:
It is also intended that BOXPLOT function works if second argument is a matrix, and in that case you can give a value for each column:
>> y = randn(100,2); boxplot(y,[1 4],'symbol','.')
However, it is an edge case where y is a single-column matrix. (i.e. X = 100x1 matrix )It qualifies as a vector, so the function expects the first syntax. The reason for the failure is that there is an older syntax for BOXPLOT function which is still working:
When the function sees a second input that looks more like a valid NOTCH value rather than either one row per vector element or one value per matrix column and since it does not recognize that a valid value, it returns an error message.
Please note, that it requires MathWorks Account to access the link.
So, the intended current and correct syntax is to provide one grouping value per element when X is a vector.
In a situation where code may receive an X that is intended to be treated as a matrix but may just happen to have one column, you can use the syntax having the grouping variables in a cell array, for example:
>> y = randn(100,1); boxplot(y,{1},'symbol','.')
>> y = randn(100,2); boxplot(y,{1:2},'symbol','.')
Hope this helps.
Sudhanshu Bhatt