0001 function [data] = vb_trial_extractor_tag_by_file(data, tag_spec)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 if ~exist('data', 'var')
0031 error('data is a required parameter.');
0032 end
0033 if ~exist('tag_spec', 'var')
0034 error('tag_spec is not specified.');
0035 end
0036 if ~isfield(tag_spec, 'label_file')
0037 error('label_file field is not specified.');
0038 end
0039 if exist(tag_spec.label_file, 'file') ~= 2
0040 error('Specified label file is invalid.');
0041 end
0042 if ~isfield(tag_spec, 'base_label')
0043 error('base_label field is not specified.');
0044 end
0045 if ischar(tag_spec.base_label)
0046 tag_spec.base_label = {tag_spec.base_label};
0047 end
0048 if ~isfield(tag_spec, 'overwrite')
0049 tag_spec.overwrite = true;
0050 end
0051
0052
0053
0054
0055
0056
0057 [label_list] = textread(tag_spec.label_file, '%s %*[^\n]');
0058 new_tags = unique(label_list);
0059
0060
0061 Ntrial = 0;
0062 base_tags = [];
0063 for k=1:length(tag_spec.base_label)
0064 base_tags = [base_tags; vb_tag_util_find_tag(data.label_list, tag_spec.base_label{k})];
0065 end
0066 base_trial_ix = [];
0067 if ~isempty(base_tags)
0068 union_base_tag = vb_tag_util_union_tag(base_tags, '', data.trial_list);
0069 base_trial_ix = vb_tag_get_trial_index(union_base_tag);
0070 Ntrial = size(base_trial_ix, 1);
0071 end
0072
0073 Ntag = length(label_list);
0074 if Ntrial ~= Ntag
0075 errordlg(...
0076 sprintf('The number of trials(%d) and labels(%d) written in label file are different. Check label file.', ...
0077 Ntrial, Ntag), 'error');
0078 return;
0079 end
0080
0081
0082 exist_tags = cell(0);
0083 for k=1:length(data.label_list)
0084 exist_tags{k} = vb_tag_get_tag_name(data.label_list{k});
0085 end
0086
0087 dup_tags = cell(0);
0088 dup_ix = [];
0089 for k=1:length(new_tags)
0090 ix = strmatch(new_tags{k}, exist_tags, 'exact');
0091 if ~isempty(ix)
0092 dup_tags{k} = new_tags{k};
0093 dup_ix = [dup_ix; ix];
0094 end
0095 end
0096
0097 if length(dup_tags) && tag_spec.overwrite == false
0098 msg = sprintf('Following tags already exists, Continue?\n');
0099 for k=1:length(dup_tags)
0100 msg = [msg, sprintf('%s\n', dup_tags{k})];
0101 end
0102 res = questdlg(msg, 'Confirm', 'Yes', 'No', 'Yes');
0103 if strcmp(lower(res), 'no')
0104 return;
0105 else
0106 data.label_list(dup_ix) = [];
0107 end
0108 end
0109
0110
0111 for k=1:length(new_tags)
0112 tag_obj = vb_tag_new(new_tags{k});
0113
0114
0115
0116 list_ix = strmatch(new_tags{k}, label_list, 'exact');
0117 data_ix = base_trial_ix(list_ix);
0118
0119
0120 tag_obj = vb_tag_add_trial_index(tag_obj, data_ix);
0121 data.label_list{length(data.label_list)+1} = tag_obj;
0122
0123 vb_disp(sprintf('created label:%s, trial=%d', new_tags{k}, length(data_ix)));
0124 end
0125
0126 data.label_spec = tag_spec;
0127
0128
0129
0130
0131 if nargout ~= 1
0132 error('function caller should receive vb_trial_extractor object.');
0133 end