<div dir="ltr">Hi Jan Mathijs,<div><br></div><div>Many thanks for your answer: it's proving to be quite some detective work but I think/hope I'm almost there.</div><div><br></div><div>First, it looks like your suggestion works for Fieldtrip itself. But perhaps you could clarify for my understanding: ft_defaults, which you call at the start of your Matlab session but is also called from various ft functions, typically checks for the presence/contents of the global ft_defaults var. But the toolbox fields are used to update (in this case, remove) paths ONLY the very first time ft_defaults is called, so have to make sure the global var has already been set. Something like that?</div><div><br></div><div>A complication I didn't mention is that I'm actually using SleepTrip, carrying a "Fieldtrip-lite" inside. Here your suggestion doesn't work, but I think I know why. (I collaborate with SleepTrip's developer so we can make changes quickly, but want to check first before I make suggestions.)</div><div><br></div><div>Inside ft_defaults there's a call </div><div><br></div><div><i>if ~ft_platform_supports('signal') || ~strcmp(ft_default.toolbox.signal, 'matlab') || ~ft_hastoolbox('signal')<br>      addpath(fullfile(fileparts(which('ft_defaults')), 'external', 'signal'));</i></div><div><i>end</i><br></div><div><br></div><div>where <i>ft_platform_supports</i> appears to verify that all functions in fieldtrip/external/signal are actually present as a Matlab builtin. Clever, but SleepTrip includes at least one function in /external/signal that has no corresponding function in Matlab's signal toolbox.</div><div><br></div><div>So my suspicion is that /external/signal (and /external/stats and /external/image) should ONLY include function names matching a function from the respective Matlab's toolbox? If so, that should be a relatively easy fix.</div><div><br></div><div>Many thanks,</div><div>Roy</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 23, 2022 at 12:26 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 Roy,
<div><br>
</div>
<div>Just changing the global ft_default variable is not going to the trick indeed. You’d need to run ft_defaults once more before the changes have an effect. Possibly you’d need to run restoredefaultpath first to be absolutely sure.</div>
<div><br>
</div>
<div>So, in summary, once you start a matlab session you’d do something like:</div>
<div><br>
</div>
<div>global ft_default;</div>
<div>ft_default.toolbox.signal = ‘matlab’;</div>
<div><br>
</div>
<div>addpath(<path-to-fieldtrip>);</div>
<div>ft_defaults;</div>
<div><br>
</div>
<div>This should do it, I think.</div>
<div><br>
</div>
<div>Best wishes and happy computing!</div>
<div><br>
</div>
<div>JM</div>
<div><br>
</div>
<div>PS: looking forward to a speed-improved version of the overloaded filtfilt.m</div>
<div><br>
</div>
<div><br>
</div>
<div>
<div><br>
<blockquote type="cite">
<div>On 22 Feb 2022, at 18:05, Roy Cox via fieldtrip <<a href="mailto:fieldtrip@science.ru.nl" target="_blank">fieldtrip@science.ru.nl</a>> wrote:</div>
<br>
<div>
<div dir="ltr">hello,
<div><br>
</div>
<div>I'm having trouble getting Fieldtrip (revision 8108d99ee.) to reliably use Matlab's builtin signal toolbox, rather than the one in Fieldtrip/external/signal. I followed the online suggestions (<a href="https://urldefense.com/v3/__https://www.fieldtriptoolbox.org/faq/should_i_add_fieldtrip_with_all_subdirectories_to_my_matlab_path/__;!!HJOPV4FYYWzcc1jazlU!9W_-dvWnmFYAlMXwYGZwfpXws4MmlqjOSG6WyVaJK6hmqIFPpthFjbCjC3mB-u0gbJolbsCaCy0-mhsceOQlVz4qTPmgPn3zU-vVgQ$" target="_blank">https://www.fieldtriptoolbox.org/faq/should_i_add_fieldtrip_with_all_subdirectories_to_my_matlab_path/</a>)
 and checked the info surrounding ft_defaults.</div>
<div><br>
</div>
<div>Background: Matab's filtfilt is about twice as fast as Fieldtrip's, which would make a big difference for our overnight sleep EEG. I tested a bandpass filter on a small recording while explicitly adding/removing
<i>Fieldtrip/external/signal</i> from the path (as NOT recommended, I know). Doing this right before filtfilt is called (from ft_preprocessing -> preproc -> ft_preproc_bandpassfilter -> filter_with_correction, line 73):</div>
<div><br>
</div>
<div>
<div>Fieldtrip</div>
<div><i>tic;filt = filtfilt(B, A, dat')';toc<br>
Elapsed time is 7.384431 seconds.</i></div>
</div>
<div><br>
</div>
<div>Matlab</div>
<div><i>tic;filt = filtfilt(B, A, dat')';toc<br>
Elapsed time is 3.328224 seconds.</i><br>
</div>
<div><i><br>
</i></div>
<div>I get similar speedups when timing ft_preprocessing, different filters, and for (much) larger datasets, so definitely worth it.</div>
<div><br>
</div>
<div>Now I try to do things the recommended way from a fresh Matlab session (with all Fieldtrip-related stuff removed from startup.m).</div>
<div><br>
</div>
<div>First I check:</div>
<div><br>
</div>
<div><i>which -all filtfilt<br>
</i></div>
<div><i>C:\Program Files\MATLAB\R2020a\toolbox\signal\signal\filtfilt.m</i></div>
<div><br>
</div>
<div>Then I initialize Fieldtrip:</div>
<div><br>
</div>
<div><i>addpath(myPathToFieldtrip)<br>
ft_defaults</i><br>
</div>
<div><br>
</div>
<div>and check filtfilt again:</div>
<div><i>which -all filtfilt<br>
longPathToFieldtrip\external\signal\filtfilt.m<br>
C:\Program Files\MATLAB\R2020a\toolbox\signal\signal\filtfilt.m            %Shadowed</i><br>
</div>
<div><br>
</div>
<div>which makes sense since the default is to use Fieldtrip's filtfilt.</div>
<div><br>
</div>
<div>I then do as suggested:</div>
<div><br>
</div>
<div><i>global ft_default<br>
ft_default.toolbox.signal='matlab';</i><br>
<br>
</div>
<div>and can verify that the global var is correctly set:</div>
<div><i>ft_default.toolbox<br>
</i></div>
<div><i><br>
</i></div>
<div><i>ans =  </i></div>
<div><i>struct with fields:<br>
<br>
     images: 'compat'<br>
      stats: 'compat'<br>
     signal: 'matlab'<br>
    cleanup: {}</i><br>
</div>
<div><i><br>
</i></div>
<div>But calling <i>which -all filtfilt </i>again, the Fieldtrip path has not been removed. I tried calling
<i>ft_defaults </i>a second time (perhaps in order for the new values in
<i>ft_default</i> to take effect?), but both filtfilt paths remain. And I verified that Fieldtrip in fact continues to use its own slow filtfilt implementation when calling
<i>ft_preprocessing</i>. So it appears that the contents of <i>
ft_default.toolbox</i> aren't actually used to update paths, which may or may not be as intended.</div>
<div><br>
</div>
<div>I have to add that at one (and only one) point during my lengthy explorations Fieldtrip DID pick the requested version (and removed the path to <i>\external\signal\ </i>). But I haven't been able to reproduce since, so this seems to work
 only under highly specific conditions.</div>
<div><br>
</div>
<div>Explicitly removing the path to <i>\external\signal\ </i>using
<i>rmpath</i> seems to work, but is also risky because I imagine Fieldtrip may attempt to restore this path depending on what functions are called elsewhere in a script (especially if the request for using the Matlab version in <i>ft_default.toolbox</i> is
 missed/ignored).</div>
<div><br>
</div>
<div>Besides physically deleting folders, how do I ensure Fieldtrip ALWAYS uses Matlab's signal (or stats/image) toolboxes and will NEVER attempt to add its own versions back to the Matlab path?</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Roy</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!9W_-dvWnmFYAlMXwYGZwfpXws4MmlqjOSG6WyVaJK6hmqIFPpthFjbCjC3mB-u0gbJolbsCaCy0-mhsceOQlVz4qTPmgPn0TBKpvfw$" target="_blank">https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!9W_-dvWnmFYAlMXwYGZwfpXws4MmlqjOSG6WyVaJK6hmqIFPpthFjbCjC3mB-u0gbJolbsCaCy0-mhsceOQlVz4qTPmgPn0TBKpvfw$</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://doi.org/10.1371/journal.pcbi.1002202" rel="noreferrer" target="_blank">https://doi.org/10.1371/journal.pcbi.1002202</a><br>
</blockquote></div>