<div dir="ltr"><div dir="ltr"><div dir="ltr">Hi <span style="color:rgb(0,0,0)">Jan-Mathijs: </span></div><div><span style="color:rgb(0,0,0)">Thank you so much for your fast response ! This is exactly the problem. There is one empty event in the bdf file. </span></div><div><span style="color:rgb(0,0,0)"><br></span></div><div><span style="color:rgb(0,0,0)">Best wishes,</span><br></div><div><span style="color:rgb(0,0,0)">Ruoyi</span></div><div><span style="color:rgb(0,0,0)"><br></span></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 6, 2023 at 10:35 PM Schoffelen, J.M. (Jan Mathijs) via fieldtrip <<a href="mailto:fieldtrip@science.ru.nl">fieldtrip@science.ru.nl</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div style="overflow-wrap: break-word;">
Hi Ruoyi,
<div><br>
</div>
<div>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:</div>
<div><br>
</div>
<div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b>value2 = {event.value}.';</b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b>value=cell2mat(value2)</b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b>sample = [event.sample].’</b></i></div>
<div><i><b><br>
</b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div><font color="#000000"><span>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.</span></font></div>
<div><font color="#000000">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...</font></div>
<div><font color="#000000"><span><br>
</span></font></div>
<div><font color="#000000"><span>Best wishes,</span></font></div>
<div><font color="#000000"><span>Jan-Mathijs</span></font></div>
<div><font color="#000000"><span><br>
</span></font><br>
<br>
<blockquote type="cite">
<div>On 6 Feb 2023, at 20:33, Ruoyi Cao via fieldtrip <<a href="mailto:fieldtrip@science.ru.nl" target="_blank">fieldtrip@science.ru.nl</a>> wrote:</div>
<br>
<div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div><b><font face="arial, sans-serif">Hello FieldTrip community,<br>
</font></b></div>
<div><b><font face="arial, sans-serif">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.</font></b></div>
<div><br>
</div>
<div><b><font face="arial, sans-serif">I am very confused as the outcome from my self-made function was surprisingly not the same. </font></b><b><font face="arial, sans-serif">Could anyone kindly give me some ideas
for why this is the case and how to fix the problem? </font></b></div>
<div><b><font face="arial, sans-serif"><br>
</font></b></div>
<div><b><font face="arial, sans-serif"><br>
</font></b></div>
<div><b><font face="arial, sans-serif">Thank you so much </font></b></div>
<div><b><font face="arial, sans-serif">Ruoyi.</font></b></div>
<div><b><font face="arial, sans-serif"><br>
</font></b></div>
<div><b><font face="arial, sans-serif">The two methods are as follows: </font></b></div>
<div><b><i><font>Standard code: </font></i></b></div>
<div><i><b><font><br>
</font></b></i></div>
<div>
<div><i><b><font>1: </font></b></i></div>
<div><i><b><font>cfg = [];</font></b></i></div>
<div><i><b><font>cfg.dataset = M</font></b></i></div>
<div><i><b><font>% cfg.trialdef.pre = 2.5;</font></b></i></div>
<div><i><b><font>% <a href="https://urldefense.com/v3/__http://cfg.trialdef.post__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXasSpsBdSQ$" target="_blank">
cfg.trialdef.post</a> = 1.5;</font></b></i></div>
<div><i><b><font>cfg.trialdef.eventtype = 'STATUS';</font></b></i></div>
<div><i><b><font>cfg.trialdef.prestim = 2.5; % in seconds</font></b></i></div>
<div><i><b><font>cfg.trialdef.poststim = 1.5; % in seconds</font></b></i></div>
<div><i><b><font>cfg.trialdef.eventvalue = 101;</font></b></i></div>
<div><i><b><font>cfg_B1 = ft_definetrial(cfg);</font></b></i></div>
<div><i><b><font>segbinding1= ft_redefinetrial(cfg_B1,data); </font></b></i></div>
</div>
<div>
<div><b><i><font>Reference: </font></i></b></div>
<div><a href="https://urldefense.com/v3/__https://www.fieldtriptoolbox.org/tutorial/continuous/__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXauX0co87g$" target="_blank"><i><b><font>Preprocessing
- Reading continuous EEG and MEG data - FieldTrip toolbox</font></b></i></a></div>
</div>
<div><br>
</div>
<div><i><b><font>2: Self made function according to following instruction : </font></b></i></div>
<div><i><b><font><br>
</font></b></i></div>
<div><i><b><font>function [trl, event] =location1(cfg);<br>
</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>hdr = ft_read_header(cfg.dataset);</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>event = ft_read_event(cfg.dataset);</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>% search for "trigger" events</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>% value = [event(find(strcmp('trigger', {event.type}))).value]';</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>value2 = {event.value}.';</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>value=cell2mat(value2)</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>% sample = [event(find(strcmp('trigger', {event.type}))).sample]';</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>sample = [event.sample].'</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>% determine the number of samples before and after the trigger</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>pretrig = -round(cfg.trialdef.pre * hdr.Fs);</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>posttrig = round(<a href="https://urldefense.com/v3/__http://cfg.trialdef.post__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXasSpsBdSQ$" target="_blank">cfg.trialdef.post</a>
* hdr.Fs);</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>% for each trigger except the last one</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>trl = [];</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>for j = 1:(length(value)-3)</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div><i><b><font> trg1 = value(j); </font></b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>if (trg1==101);</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>trlbegin = sample(j) + pretrig;</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>trlend = sample(j) + posttrig;</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>offset = pretrig;</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>newtrl = [trlbegin trlend offset ];;</font></b></i></div>
<div><i><b><font> trl = [trl; newtrl] </font></b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
<div dir="ltr">
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<div>
<div>
<div>
<div><i><b><font>end</font></b></i></div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div><i><b><font>end</font></b></i></div>
<div><i><b><font><br>
</font></b></i></div>
<div><i><b><font>This function was run with : </font></b></i></div>
<div>
<div><i><b><font>cfg = [];</font></b></i></div>
<div><i><b><font>cfg.dataset = M</font></b></i></div>
<div><i><b><font>cfg.trialdef.pre = 2.5;</font></b></i></div>
<div><i><b><font><a href="https://urldefense.com/v3/__http://cfg.trialdef.post__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXasSpsBdSQ$" target="_blank">cfg.trialdef.post</a>
= 1.5;</font></b></i></div>
<div><i><b><font>cfg.trialfun = 'location1';</font></b></i></div>
<div><i><b><font>cfg_B1 = ft_definetrial(cfg);</font></b></i></div>
<div><i><b><font>segbinding1= ft_redefinetrial(cfg_B1,data);</font></b></i></div>
</div>
<div><i><b><font>References </font></b></i></div>
<div><i><b><font><a href="https://urldefense.com/v3/__https://www.fieldtriptoolbox.org/example/making_your_own_trialfun_for_conditional_trial_definition/__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXatuNjCdqQ$" target="_blank">Making
your own trialfun for conditional trial definition - FieldTrip toolbox</a><br>
</font></b></i></div>
<div><b><font face="comic sans ms, sans-serif"><i><br>
</i></font></b></div>
<div><b><font face="arial narrow, sans-serif"><i>The 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. </i></font></b></div>
<div><i><b><font><br>
</font></b></i></div>
<div><i><b><font><span id="m_5986659972676819536cid:ii_ldt71xeb2"><image.png></span></font></b></i><br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
_______________________________________________<br>
fieldtrip mailing list<br>
<a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" target="_blank">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
<a href="https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!_MVGSW6yRgkWvGt4xy4cmB_vlZyD-pBdlbVepuQMeFvwrOtYJgIGHAAo0UbHZSnMQFmpY44JpbC233m34wdGVGr04n82eQoxcxg4aA$" target="_blank">https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!_MVGSW6yRgkWvGt4xy4cmB_vlZyD-pBdlbVepuQMeFvwrOtYJgIGHAAo0UbHZSnMQFmpY44JpbC233m34wdGVGr04n82eQoxcxg4aA$</a>
<br>
</div>
</blockquote>
</div>
<br>
</div>
</div>
_______________________________________________<br>
fieldtrip mailing list<br>
<a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" rel="noreferrer" target="_blank">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
<a href="https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!_CbrzMjS8UupcSJQiDc0f0JqnvhhEthw70my2Lkjee7fPGr_4Ouk3t3z6lBeS9O6Sk3kK714IbE5ro_wW6-Z4jR9SKmjcA$" rel="noreferrer" target="_blank">https://doi.org/10.1371/journal.pcbi.1002202</a><br>
</blockquote></div>