PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/examples/toolboxes/fieldtrip/ft_definetrial.m

http://brainstream.googlecode.com/
MATLAB | 166 lines | 50 code | 12 blank | 104 comment | 9 complexity | 892ff52130832a9ef4bae28e9a7651f5 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, LGPL-2.0, GPL-3.0, GPL-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  1. function [cfg] = ft_definetrial(cfg);
  2. % FT_DEFINETRIAL defines the trials, i.e. the pieces of data that will be read
  3. % in for preprocessing. Trials are defined by their begin and end sample
  4. % in the data file and each trial has an offset that defines where the
  5. % relative t=0 point (usually the point of the trigger) is for that trial.
  6. %
  7. % Use as
  8. % [cfg] = ft_definetrial(cfg)
  9. % where the configuration structure should contain either
  10. % cfg.trialdef = structure with details of trial definition, see below
  11. % cfg.trialfun = function name, see below
  12. %
  13. % A call to FT_DEFINETRIAL results in the trial definition "trl" being added
  14. % to the output configuration structure. The trials are defined according
  15. % to the triggers, trials or other events in the data, or from a
  16. % user-specified Matlab function which returns "trl".
  17. %
  18. % The trial definition "trl" is an Nx3 matrix, N is the number of trials.
  19. % The first column contains the sample-indices of the begin of each trial
  20. % relative to the begin of the raw data, the second column contains the
  21. % sample-indices of the end of each trial, and the third column contains
  22. % the offset of the trigger with respect to the trial. An offset of 0
  23. % means that the first sample of the trial corresponds to the trigger. A
  24. % positive offset indicates that the first sample is later than the trigger,
  25. % a negative offset indicates that the trial begins before the trigger.
  26. %
  27. % Simple trial definitions (e.g. based on a trigger alone) are supported by
  28. % FT_DEFINETRIAL itself. For this, the general and data format independent way
  29. % of handling trials is by relying on the FT_READ_EVENT function to
  30. % collect all event information (such as triggers) from your dataset and
  31. % select trials based on those events. This is implemented in FT_DEFINETRIAL as
  32. % cfg.trialdef.eventtype = 'string'
  33. % cfg.trialdef.eventvalue = number, string or list with numbers or strings
  34. % cfg.trialdef.prestim = number, latency in seconds (optional)
  35. % cfg.trialdef.poststim = number, latency in seconds (optional)
  36. %
  37. % If you specify cfg.trialdef.eventtype = '?' a list with the events in your
  38. % data file will be displayed on screen.
  39. %
  40. % However, there are also many other complex ways in which you can define
  41. % data pieces of interest, for example based on a conditional sequence of
  42. % events (e.g. stimulus trigger followed by a correct response). For those
  43. % cases, a general mechanism has been implemented through which you can
  44. % supply your own trial-defining function, the 'trialfun'.
  45. %
  46. % This 'trialfun' is a string containing the name of a function that you
  47. % have to write yourself. The function should take the cfg-structure as
  48. % input and should give a Nx3 matrix in the same format as "trl" as the
  49. % output. You can add extra custom fields to the configuration structure to
  50. % pass as arguments to your own trialfun. Furthermore, inside the trialfun
  51. % you can use the FT_READ_EVENT function to get the event information
  52. % from your data file.
  53. %
  54. % See also FT_PREPROCESSING, FT_READ_HEADER, FT_READ_DATA, FT_READ_EVENT
  55. % Undocumented local options:
  56. % cfg.datafile
  57. % cfg.dataset
  58. % cfg.event
  59. % cfg.trl
  60. % cfg.version
  61. % Copyright (c) 2003, Robert Oostenveld, F.C. Donders Centre
  62. %
  63. % This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
  64. % for the documentation and details.
  65. %
  66. % FieldTrip is free software: you can redistribute it and/or modify
  67. % it under the terms of the GNU General Public License as published by
  68. % the Free Software Foundation, either version 3 of the License, or
  69. % (at your option) any later version.
  70. %
  71. % FieldTrip is distributed in the hope that it will be useful,
  72. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  73. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  74. % GNU General Public License for more details.
  75. %
  76. % You should have received a copy of the GNU General Public License
  77. % along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
  78. %
  79. % $Id: ft_definetrial.m 948 2010-04-21 18:02:21Z roboos $
  80. fieldtripdefs
  81. % check if the input cfg is valid for this function
  82. cfg = checkconfig(cfg, 'trackconfig', 'on');
  83. cfg = checkconfig(cfg, 'dataset2files', {'yes'});
  84. if ~isfield(cfg, 'trl') && (~isfield(cfg, 'trialfun') || isempty(cfg.trialfun))
  85. % there used to be other system specific trialfuns in previous versions
  86. % of fieldtrip, but they are deprecated and not included in recent
  87. % versions any more
  88. cfg.trialfun = 'trialfun_general';
  89. warning('no trialfun was specified, using trialfun_general');
  90. end
  91. % create the trial definition for this dataset and condition
  92. if isfield(cfg, 'trl')
  93. % the trial definition is already part of the configuration
  94. fprintf('retaining exist trial definition\n');
  95. trl = cfg.trl;
  96. if isfield(cfg, 'event')
  97. fprintf('retaining exist event information\n');
  98. event = cfg.event;
  99. else
  100. event = [];
  101. end
  102. elseif isfield(cfg, 'trialfun')
  103. % evaluate the user-defined function that gives back the trial definition
  104. fprintf('evaluating trialfunction ''%s''\n', cfg.trialfun);
  105. % determine the number of outpout arguments of the user-supplied trial function
  106. try
  107. % the nargout function in Matlab 6.5 and older does not work on function handles
  108. num = nargout(cfg.trialfun);
  109. catch
  110. num = 1;
  111. end
  112. if num==1
  113. % the user-defined function only gives back the trial definition
  114. trl = feval(cfg.trialfun, cfg);
  115. event = [];
  116. else
  117. % the user-defined function also gives back detailed information about
  118. % conditions, reaction time or any other information
  119. [trl, event] = feval(cfg.trialfun, cfg);
  120. end
  121. else
  122. error('no trialfunction specified, see DEFINETRIAL for help');
  123. end
  124. if isfield(cfg, 'trialdef') && isfield(cfg.trialdef, 'eventtype') && strcmp(cfg.trialdef.eventtype, '?')
  125. % give a gentle message instead of an error
  126. fprintf('no trials have been defined yet, see DEFINETRIAL for further help\n');
  127. elseif size(trl,1)<1
  128. error('no trials were defined, see DEFINETRIAL for help');
  129. end
  130. % add the new trials and events to the output configuration
  131. fprintf('found %d events\n', length(event));
  132. cfg.event = event;
  133. fprintf('created %d trials\n', size(trl,1));
  134. cfg.trl = trl;
  135. % get the output cfg
  136. cfg = checkconfig(cfg, 'trackconfig', 'off', 'checksize', 'yes');
  137. % add information about the version of this function to the configuration
  138. try
  139. % get the full name of the function
  140. cfg.version.name = mfilename('fullpath');
  141. catch
  142. % required for compatibility with Matlab versions prior to release 13 (6.5)
  143. [st, i1] = dbstack;
  144. cfg.version.name = st(i1);
  145. end
  146. cfg.version.id = '$Id: ft_definetrial.m 948 2010-04-21 18:02:21Z roboos $';
  147. % % remember the exact configuration details in the output
  148. % cfgtmp = cfg;
  149. % cfg = [];
  150. % try cfg.trl = cfgtmp.trl; end
  151. % try cfg.dataset = cfgtmp.dataset; end
  152. % try cfg.datafile = cfgtmp.datafile; end
  153. % try cfg.headerfile = cfgtmp.headerfile; end
  154. % cfg.previous = cfgtmp;