[FieldTrip] using Matlab instead of Fieldtrip toolboxes

Schoffelen, J.M. (Jan Mathijs) janmathijs.schoffelen at donders.ru.nl
Wed Feb 23 12:22:35 CET 2022

Hi Roy,

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.

So, in summary, once you start a matlab session you’d do something like:

global ft_default;
ft_default.toolbox.signal = ‘matlab’;


This should do it, I think.

Best wishes and happy computing!


PS: looking forward to a speed-improved version of the overloaded filtfilt.m

On 22 Feb 2022, at 18:05, Roy Cox via fieldtrip <fieldtrip at science.ru.nl<mailto:fieldtrip at science.ru.nl>> wrote:


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 (https://www.fieldtriptoolbox.org/faq/should_i_add_fieldtrip_with_all_subdirectories_to_my_matlab_path/<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$>) and checked the info surrounding ft_defaults.

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 Fieldtrip/external/signal 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):

tic;filt = filtfilt(B, A, dat')';toc
Elapsed time is 7.384431 seconds.

tic;filt = filtfilt(B, A, dat')';toc
Elapsed time is 3.328224 seconds.

I get similar speedups when timing ft_preprocessing, different filters, and for (much) larger datasets, so definitely worth it.

Now I try to do things the recommended way from a fresh Matlab session (with all Fieldtrip-related stuff removed from startup.m).

First I check:

which -all filtfilt
C:\Program Files\MATLAB\R2020a\toolbox\signal\signal\filtfilt.m

Then I initialize Fieldtrip:


and check filtfilt again:
which -all filtfilt
C:\Program Files\MATLAB\R2020a\toolbox\signal\signal\filtfilt.m            %Shadowed

which makes sense since the default is to use Fieldtrip's filtfilt.

I then do as suggested:

global ft_default

and can verify that the global var is correctly set:

ans =
struct with fields:

     images: 'compat'
      stats: 'compat'
     signal: 'matlab'
    cleanup: {}

But calling which -all filtfilt again, the Fieldtrip path has not been removed. I tried calling ft_defaults a second time (perhaps in order for the new values in ft_default 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 ft_preprocessing. So it appears that the contents of ft_default.toolbox aren't actually used to update paths, which may or may not be as intended.

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 \external\signal\ ). But I haven't been able to reproduce since, so this seems to work only under highly specific conditions.

Explicitly removing the path to \external\signal\ using rmpath 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 ft_default.toolbox is missed/ignored).

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?

fieldtrip mailing list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20220223/9e4800fa/attachment.htm>

More information about the fieldtrip mailing list