Possible to run mrtrix executables from matlab?


Dear Mrtrix developers and users,

I was wondering whether other people also use matlab to write scripts for executing the mtrix commands?
Typically executables are executed by using the system(command.exe) function of matlab.
However, I experience that matlab does not execute my mrtrix commands:
e.g. if I run system(‘mrconvert A01_DWI.nii A01_DWI.mif’), no new .mif file is constructed.
e.g. if I run system(‘mrconvert5769 A01_DWI.nii A01_DWI.mif’), matlab gives the error that command mrconvert5769 could nog be found
So the commands of mrtrix e.g. mrconvert are recognized, but not executed.
When I execute the command from a shell script, everything works fine.
Also, when I run another executable (not from mrtrix) from matlab with system(), this works fine.

I could not find why the commands of mrtrix could not be executed correctly with the system() function in matlab?

Thank you for your efforts in advance!

With kind regards,
Charlotte Sleurs


Hi Charlotte,

If you call any command with matlab system function, it needs to choose a shell (e.g. bash or tcsh). Since you likely set up your system PATH in either .bashrc or .tcshrc, you need to tell matlab to use the same one, otherwise it won’t find mrconvert.

According to the matlab docs:
On UNIX, MATLAB uses a shell program to execute the given command. It determines which shell program to use by checking environment variables on your system. MATLAB first checks the MATLAB_SHELL variable,
and if either empty or not defined, then checks SHELL. If SHELL is also empty or not defined, MATLAB uses /bin/sh.

My best guess is that your setup is currently using the default sh. Therefore, if you add export SHELL=/bin/bash to your .bashrc or setenv SHELL /bin/tcsh to your .tcshrc file (depending on which one you’re using), and restart matlab, that should fix it. If you’re unsure which shell you’re using, check echo $0 from a terminal.



This doesn’t look like a PATH problem, since the first command (which invokes mrconvert) runs without errors reported, whereas the second (with a nonsense executable) does show the expected ‘command not found’ error.

I think this is likely to be a mismatch in the GLIBC version - a problem we’ve encountered here too. In a nutshell, GLIBC is a fundamental system library which just about all executables will use. Matlab however uses its own (pretty outdated) version of this library, and sets the LD_LIBRARY_PATH environment variable to instruct the system to look in its own folders first for that library - a pretty ugly hack. What this means for you is that since this variable is also inherited by any command issued using the system() call, the command invoked will be provided with Matlab’s version of GLIBC rather than the normal system one. This typically leads to the commands failing to run with a GLIBC version mismatch error (I’m surprised you’re not seeing such a message?).

To get around this, we’ve resorted to prefixing every command in the system() call with LD_LIBRARY_PATH= to reset the problematic environment variable. So your call would be modified from e.g.:

system('mrconvert A01_DWI.nii A01_DWI.mif')


system('LD_LIBRARY_PATH= mrconvert A01_DWI.nii A01_DWI.mif')

Give it a try, see if that solves the problem for you…


Hi Daan and j.Donald,

Thank you for your answers. Indeed, if I run system(‘PATH’),
I get the anwer PATH=C:\Program Files\MATLAB\R2016a\bin\win64;C:\msys64\mingw64\bin;C:\msys64\mingw64\lib; … in matlab, so I think the path indeed is fine.

I tried the suggestion of j.Donald and received ans=0 (instead of -1.0737e+09 which I received earlier),
which would now state that the command has runned properly…
However, unfortunately no new .mif file is created…


Well I guess at least that’s progress… I don’t understand how this can happen though: an exit code of 0 indicates success, so it should have produced the file successfully. Any error in the command should give an exit status of 1 (at least, any error that we actually handle). Are you not getting any terminal output from the command? And does the same command actually work as expected when run directly from the terminal…?


Absolutely, the command works fine from the terminal… It seems that it has to do with the communication between matlab and terminal… To solve it for now, I started shell scripting, like eveyone else :wink:


Hello everyone,

Some colleagues and I were also looking for solution to use MRtrix3 binaries in MATLAB.
The command suggested above do the job for me on a standard linux (ubuntu).
In MATLAB terminal : system('LD_LIBRARY_PATH= mrview')

If I don’t prefix the command with LD_LIBRARY_PATH=, then I have an error message related to Qt. I think the reason is MATLAB uses it’s own Qt libraries, which are different than the linux system. And MATLAB Qt libraries locations are defined in LD_LIBRARY_PATH…

By the way, this trick also works with FSL binaries : system('LD_LIBRARY_PATH= fslview')



Hi Charlotte,

Changing my startup.m file (Users/$username/Documents/MATLAB/startup.m) fixed this problem on my Mac. I added the following command:

setenv(‘BASH_ENV’, ‘~/.bash_profile’);

Hope this helps!