diff --git a/violinplot.m b/violinplot.m index 8b75c8c..c151312 100644 --- a/violinplot.m +++ b/violinplot.m @@ -51,19 +51,21 @@ % This code is released under the terms of the BSD 3-clause license hascategories = exist('cats','var') && not(isempty(cats)); + + %parse the optional grouporder argument + %if it exists parse the categories order + % but also delete it from the arguments passed to Violin grouporder = {}; - ii=1; - while ii<=numel(varargin) - if strcmp(varargin{ii},'GroupOrder') - grouporder = varargin{ii+1}; - varargin(ii:ii+1)=[]; - ii=ii+2; + idx=find(strcmp(varargin, 'GroupOrder')); + if ~isempty(idx) && numel(varargin)>idx + if iscell(varargin{idx+1}) + grouporder = varargin{idx+1}; + varargin(idx:idx+1)=[]; else - ii=ii+1; + error('Second argument of ''GroupOrder'' optional arg must be a cell of category names') end end - % tabular data if isa(data, 'dataset') || isstruct(data) || istable(data) if isa(data, 'dataset')