Guten Tag, Hi, Ich bin vom 05.02.2023 bis 10.02.2023 nicht im Büro und kann die Emails nur sehr unregelmäßig lesen und beantworten. Die Emails werden auch nicht weitergeleitet. I am out of the office from 05.02.2023 to 10.02.2023 and can only read and answer emails very sporadically. The emails are also not being forwarded. Viele Grüße / Best regards Stephan Frisch Am 07.02.2023 um 00:32 schrieb Ruoyi Cao via fieldtrip <: 0000,0000,0000Hi Jan-Mathijs:  Thank you so much for your fast response ! This is exactly the problem. There is one empty event in the bdf file.  Best wishes, Ruoyi On Mon, Feb 6, 2023 at 10:35 PM Schoffelen, J.M. (Jan Mathijs) via fieldtrip <<08B3,4F87,D1D1fieldtrip@science.ru.nl0000,0000,0000> wrote: Hi Ruoyi, I don’t know about the specific file format you are working with, but - occasionally in some file formats - some events may have an empty value. As a consequence, the code that you used in your selfmade function may not have been absolutely robust. Specifically, the three lines: value2 = {event.value}.'; value=cell2mat(value2) sample = [event.sample].’ lead to two vectors (value and sample) that are assumed to relate to each other one-by-one, meaning that element -i- in one of the vectors corresponds to element -i- in the other vector. I would first check if this is really the case in your data. If not, I think that this explains the discrepancy you report, and would be indicative that you may need to change your own code a bit, in order to obtain the expected behavior. You could quickly check this: a = {1 2 [] 3}; b = cell2mat(a); As you will see, ‘a’ has 4 elements, ‘b’ only 3... Best wishes, Jan-Mathijs 45BA,3BDE,CD06On 6 Feb 2023, at 20:33, Ruoyi Cao via fieldtrip <<08B3,4F87,D1D1fieldtrip@science.ru.nl45BA,3BDE,CD06> wrote: ArialHello FieldTrip community, I segmented data by triggers after artifact correction on continuous data , where all deleted parts were replaced with NaN. Then this data mat was segmented with ft_trialfun_general and with a self made function as follows. The NaN value was later deleted after the output was time locked to the trigger. ArialI am very confused as the outcome from my self-made function was surprisingly not the same. Could anyone kindly give me some ideas for why this is the case and how to fix the problem?  Arial Thank you so much  ArialRuoyi. Arial The two methods are as follows:  Standard code:  1:  cfg = []; cfg.dataset             = M % cfg.trialdef.pre    =  2.5; % 08B3,4F87,D1D1cfg.trialdef.post45BA,3BDE,CD06   =  1.5;45BA,3BDE,CD06 cfg.trialdef.eventtype      = 'STATUS'; cfg.trialdef.prestim        = 2.5; % in seconds cfg.trialdef.poststim       = 1.5; % in seconds cfg.trialdef.eventvalue = 101; cfg_B1 = ft_definetrial(cfg); segbinding1= ft_redefinetrial(cfg_B1,data);  Reference:  08B3,4F87,D1D1Preprocessing - Reading continuous EEG and MEG data - FieldTrip toolbox45BA,3BDE,CD06 2:  Self made function according to following instruction :  function [trl, event] =location1(cfg); hdr   = ft_read_header(cfg.dataset); event = ft_read_event(cfg.dataset); % search for "trigger" events % value  = [event(find(strcmp('trigger', {event.type}))).value]'; value2 = {event.value}.'; value=cell2mat(value2) % sample = [event(find(strcmp('trigger', {event.type}))).sample]'; sample = [event.sample].' % determine the number of samples before and after the trigger pretrig  = -round(cfg.trialdef.pre  * hdr.Fs); posttrig =  round(08B3,4F87,D1D1cfg.trialdef.post45BA,3BDE,CD06 * hdr.Fs);45BA,3BDE,CD06 % for each trigger except the last one trl = []; for j = 1:(length(value)-3)            trg1 = value(j);  if (trg1==101); trlbegin = sample(j) + pretrig; trlend   = sample(j) + posttrig; offset   = pretrig; newtrl   = [trlbegin trlend  offset ];;  trl      = [trl; newtrl]    end end This function was run with :  cfg = []; cfg.dataset             = M cfg.trialdef.pre    =  2.5; 08B3,4F87,D1D1cfg.trialdef.post45BA,3BDE,CD06   =  1.5;45BA,3BDE,CD06 cfg.trialfun = 'location1'; cfg_B1 = ft_definetrial(cfg); segbinding1= ft_redefinetrial(cfg_B1,data); References  08B3,4F87,D1D1Making your own trialfun for conditional trial definition - FieldTrip toolbox45BA,3BDE,CD06 Comic Sans MS45BA,3BDE,CD06 Arial Narrow45BA,3BDE,CD06The following excels shows two different trial definitions given by these two methods. The highlighted value is the differences between the beginning of each segment for each detected trial between two methods. 45BA,3BDE,CD06 < _______________________________________________ fieldtrip mailing list 08B3,4F87,D1D1https://mailman.science.ru.nl/mailman/listinfo/fieldtrip45BA,3BDE,CD06 08B3,4F87,D1D1https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!_MVGSW6yRgkWvGt4xy4cmB_vlZyD-pBdlbVepuQMeFvwrOtYJgIGHAAo0UbHZSnMQFmpY44JpbC233m34wdGVGr04n82eQoxcxg4aA$45BA,3BDE,CD06 0000,0000,0000 _______________________________________________ fieldtrip mailing list 08B3,4F87,D1D1https://mailman.science.ru.nl/mailman/listinfo/fieldtrip0000,0000,0000 08B3,4F87,D1D1https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!8SCcS8FlnYiPu9lRawM9p2yQzQ3ZfW9p7RXL6-iVu0Mc7zMEq-o81moGryXuLenJyFYachVb-8-LYfmIlDM1uncJ1JkLlf5deA$ 0000,0000,0000 _______________________________________________ fieldtrip mailing list https://mailman.science.ru.nl/mailman/listinfo/fieldtrip https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!8SCcS8FlnYiPu9lRawM9p2yQzQ3ZfW9p7RXL6-iVu0Mc7zMEq-o81moGryXuLenJyFYachVb-8-LYfmIlDM1uncJ1JkLlf5deA$