0001 function annot=bml_annot_table(x, description, x_var_name)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 if isempty(x)
0019 x=table();
0020 end
0021
0022 if isnumeric(x)
0023 if size(x,1) < size(x,2)
0024 x = x';
0025 end
0026 x = table(x);
0027 end
0028 if iscell(x); x=cell2table(x); end
0029 if isstruct(x); x=struct2table(x); end
0030
0031 if ~exist('description','var') || isempty(description)
0032 if isempty(x.Properties.Description)
0033 if exist('x_var_name','var')
0034 description=x_var_name;
0035 else
0036 description=inputname(1);
0037 end
0038 else
0039 description=x.Properties.Description;
0040 end
0041 end
0042
0043 if iscellstr(description)
0044 if numel(description)==1
0045 description = description{1};
0046 else
0047 error('The Description property must be a character vector.');
0048 end
0049 end
0050
0051 if isempty(x)
0052 annot=table();
0053 annot.Properties.Description = description;
0054 return
0055 end
0056
0057 if ~ismember('starts',x.Properties.VariableNames)
0058 if width(x)<=2
0059 x.Properties.VariableNames(1)={'starts'};
0060 else
0061 error('x should have variable ''starts''');
0062 end
0063 end
0064 if(any(ismissing(x.starts)))
0065 warning("starts contains nans");
0066 end
0067
0068 if ~ismember('ends',x.Properties.VariableNames)
0069 if width(x)==1
0070 x.ends = x.starts;
0071 elseif width(x)==2
0072 x.Properties.VariableNames(2)={'ends'};
0073 else
0074 error('x should have variable ''ends''');
0075 end
0076 end
0077 if(any(ismissing(x.ends)))
0078 warning("ends contains nans");
0079 end
0080
0081 if ~strcmp('id',x.Properties.VariableNames)
0082 x = sortrows(x,'starts');
0083 x = [table(transpose(1:height(x)),'VariableNames',{'id'}), x];
0084 elseif length(unique(x.id)) < height(x)
0085 error('inconsistent id variable')
0086 else
0087 x = sortrows(x,'id');
0088 end
0089
0090 if any(strcmp('duration',x.Properties.VariableNames))
0091 x.duration = [];
0092 end
0093 x = [x, table(x.ends - x.starts,'VariableNames',{'duration'})];
0094
0095 annot = bml_annot_reorder_vars(x, {'id','starts','ends','duration'});
0096 annot.Properties.Description = description;
0097
0098