# Possible to run mrtrix executables from matlab?

**URL:** <https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430>\
**Category:** Uncategorized\
**Created:** [September 8, 2016, 9:20am UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430 "2016-09-08T09:20:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charlotte](https://community.mrtrix.org/user_avatar/community.mrtrix.org/charlotte/32/264_2.png) [@Charlotte](https://community.mrtrix.org/u/Charlotte)\
**Post date:** [September 8, 2016, 9:20am UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/1 "2016-09-08T09:20:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dchristiaens](https://community.mrtrix.org/user_avatar/community.mrtrix.org/dchristiaens/32/2347_2.png) [@dchristiaens](https://community.mrtrix.org/u/dchristiaens)\
**Post date:** [September 8, 2016, 9:53am UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/2 "2016-09-08T09:53:38Z")

</div>

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](http://www.mathworks.com/help/matlab/ref/system.html):  
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.

Cheers,  
Daan

---

<div class="post-metadata">

**Author:** ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)\
**Post date:** [September 8, 2016, 10:45am UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/3 "2016-09-08T10:45:00Z")

</div>

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')

```

to

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

```

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

---

<div class="post-metadata">

**Author:** ![Charlotte](https://community.mrtrix.org/user_avatar/community.mrtrix.org/charlotte/32/264_2.png) [@Charlotte](https://community.mrtrix.org/u/Charlotte)\
**Post date:** [September 9, 2016, 11:58am UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/4 "2016-09-09T11:58:39Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)\
**Post date:** [September 9, 2016, 4:24pm UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/5 "2016-09-09T16:24:07Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![Charlotte](https://community.mrtrix.org/user_avatar/community.mrtrix.org/charlotte/32/264_2.png) [@Charlotte](https://community.mrtrix.org/u/Charlotte)\
**Post date:** [September 21, 2016, 8:57am UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/6 "2016-09-21T08:57:13Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![benoitberanger](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@benoitberanger](https://community.mrtrix.org/u/benoitberanger)\
**Post date:** [June 8, 2017, 6:57pm UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/7 "2017-06-08T18:57:48Z")

</div>

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')`

Cheers,  
Benoît

---

<div class="post-metadata">

**Author:** ![Corinne\_McGill](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@Corinne\_McGill](https://community.mrtrix.org/u/Corinne_McGill)\
**Post date:** [March 27, 2018, 6:03pm UTC](https://community.mrtrix.org/t/possible-to-run-mrtrix-executables-from-matlab/430/8 "2018-03-27T18:03:09Z")

</div>

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!  
Corinne
