Home > functions > gui > preAnalysis > trial_extractor > vb_trial_extractor_tag_by_file.m

vb_trial_extractor_tag_by_file

PURPOSE ^

tag trials by file.

SYNOPSIS ^

function [data] = vb_trial_extractor_tag_by_file(data, tag_spec)

DESCRIPTION ^

 tag trials by file.

 [USAGE]%  [data] = vb_trial_extractor_tag_by_file(data, tag_spec);
 [IN]
        data : vb_trial_extractor object.
    tag_spec : tag specification
         .label_file : text file. This file contains tag list.
                       each line has one tag.
                1
                3
                2
                1
                ...
         .base_label   : <<cell>> specify tags which is tagged.
                       example: {'TRIGGER1', 'TRIGGER2'};
         .overwrite : [true] or false
                       true : overwrite label without saying.
                      false : a dialog is shown when overwrite label.

 [OUT]
    data : vb_trial_extractor object.

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data] = vb_trial_extractor_tag_by_file(data, tag_spec)
0002 % tag trials by file.
0003 %
0004 % [USAGE]%  [data] = vb_trial_extractor_tag_by_file(data, tag_spec);
0005 % [IN]
0006 %        data : vb_trial_extractor object.
0007 %    tag_spec : tag specification
0008 %         .label_file : text file. This file contains tag list.
0009 %                       each line has one tag.
0010 %                1
0011 %                3
0012 %                2
0013 %                1
0014 %                ...
0015 %         .base_label   : <<cell>> specify tags which is tagged.
0016 %                       example: {'TRIGGER1', 'TRIGGER2'};
0017 %         .overwrite : [true] or false
0018 %                       true : overwrite label without saying.
0019 %                      false : a dialog is shown when overwrite label.
0020 %
0021 % [OUT]
0022 %    data : vb_trial_extractor object.
0023 %
0024 % Copyright (C) 2011, ATR All Rights Reserved.
0025 % License : New BSD License(see VBMEG_LICENSE.txt)
0026 
0027 %
0028 % --- Previous check
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 % --- Main Procedure
0054 %
0055 
0056 % Read tag file
0057 [label_list] = textread(tag_spec.label_file, '%s %*[^\n]');
0058 new_tags   = unique(label_list);
0059 
0060 % Check the number of trials which is specifid tag.
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 % Duplicate check between new tags and exist tags
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) = []; % remove duplicate tag.
0107    end
0108 end
0109 
0110 % Create new tag and register trial index
0111 for k=1:length(new_tags)
0112     tag_obj = vb_tag_new(new_tags{k});
0113     
0114 
0115     % extract data index
0116     list_ix = strmatch(new_tags{k}, label_list, 'exact');
0117     data_ix = base_trial_ix(list_ix);
0118 
0119     % register trial
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 % --- After check
0130 %
0131 if nargout ~= 1
0132     error('function caller should receive vb_trial_extractor object.');
0133 end

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005