Skip to content

Instantly share code, notes, and snippets.

@sheljohn
Created April 9, 2016 22:48
Show Gist options
  • Save sheljohn/a77702e3de1ef8892b7f62665d1d33f5 to your computer and use it in GitHub Desktop.
Save sheljohn/a77702e3de1ef8892b7f62665d1d33f5 to your computer and use it in GitHub Desktop.
Fixing FieldTrip issues with Matlab
  • Change directory to fieldtrip, delete all Mex files in the src/ directory
  • Edit src/ft_getopt.c and replace any mxErrMsgTxt to mexErrMsgTxt (notice the 'mex' instead of 'mx')
  • Rename mxSerialize.c to mxSerialize.cpp (same for mxDeserialize)
  • Edit those files, and replace the prototype declarations to:
// MX_API_VER has unfortunately not changed between R2013b and R2014a,
// so we use the new MATRIX_DLL_EXPORT_SYM as an ugly hack instead
#if defined(__cplusplus) && defined(MATRIX_DLL_EXPORT_SYM)
    namespace matrix{ namespace detail{ namespace noninlined{ namespace mx_array_api{
#endif
 
EXTERN_C mxArray* mxSerialize(mxArray const *);
EXTERN_C mxArray* mxDeserialize(const void *, size_t);
// and so on, for any other MEX C functions that migrated to C++ in R2014a
 
#if defined(__cplusplus) && defined(MATRIX_DLL_EXPORT_SYM)
    }}}}
    using namespace matrix::detail::noninlined::mx_array_api;
#endif

(source: http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data)

  • run ft_compile_mex.m, you might get warnings/errors in text, but the script should finish executing without error
  • then replace all Mex files in FieldTrip with the newly compiled ones:
[~,mex_files] = system(['find . -type f -name "*.' mexext '"']);
not_src = cellfun( @isempty, strfind(mex_files,'src/') );
to_replace = mex_files(not_src);
src_files = mex_files(~not_src);

for i = 1:numel(to_replace)
    [p,n,e] = fileparts(to_replace{i}); 
    [~,k]=ismember(n,src_files_names); 
    if k > 0
        disp([ fullfile(p,[n e]) ' will be replaced by ' src_files{k} ]); 
        copyfile(src_files{k},fullfile(p,[n e])); 
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment