0001 function raw = bml_annot2raw(cfg, annot)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 if istable(cfg)
0023 cfg = struct('roi',cfg);
0024 elseif isstruct(cfg) && all(ismember({'trial','time','label'},fieldnames(cfg)))
0025 cfg = struct('template',cfg);
0026 end
0027 roi = bml_getopt(cfg,'roi');
0028 template = bml_getopt(cfg,'template');
0029 label = bml_getopt(cfg,'label',{'annot'});
0030
0031 assert(~isempty(roi) || ~isempty(template),'cfg.roi or cfg.template required');
0032
0033
0034 if ~isempty(roi)
0035 raw=[];
0036 raw.time=cell(1,height(roi));
0037 raw.trial=cell(1,height(roi));
0038 raw.fsample=roi.Fs(1);
0039 raw.label = label;
0040 assert(length(unique(roi.Fs))==1, "Inconsistent Fs in roi");
0041 for i=1:height(roi)
0042
0043 raw.time{i}=bml_idx2time(roi(i,:),roi.s1(i):roi.s2(i));
0044 raw.trial{i}=zeros(length(label),size(raw.time{1},2));
0045 end
0046 elseif ~isempty(template)
0047 raw = template;
0048 for i=1:length(raw.trial)
0049 raw.trial{i}=zeros(length(label),size(raw.time{i},2));
0050 end
0051 roi = bml_raw2annot(raw);
0052 else
0053 error('cfg.roi or cfg.template required');
0054 end
0055
0056 if nargin == 2
0057 annot = bml_annot_table(annot,[],inputname(2));
0058
0059
0060
0061
0062
0063
0064 description = annot.Properties.Description;
0065 if isempty(description)
0066 description = 'annot';
0067 end
0068 if length(raw.label)==1 && strcmp(raw.label{1},'annot')
0069 raw.label={description};
0070 end
0071 annot_label = bml_getopt(cfg,'annot_label',raw.label{1});
0072 annot_idx = bml_getidx(annot_label,raw.label);
0073
0074 for t=1:height(roi)
0075 t_annot = bml_annot_filter(annot,roi(t,:));
0076 for i=1:height(t_annot)
0077 [s,e] = bml_crop_idx_valid(roi(t,:), t_annot.starts(i), t_annot.ends(i));
0078 raw.trial{t}(annot_idx,s:e)=1;
0079 end
0080 end
0081 end
0082
0083
0084
0085