Command 'matlab' could not be found on host

Hi! I ran into some bug when I run nipype.interfaces.spm. Here is my code:

import nipype.interfaces.spm as spm
from nipype.interfaces.matlab import MatlabCommand
seg = spm.NewSegment()
seg.inputs.channel_files = ‘X_train_file_0.nii’
seg.run()

The output is:

OSError Traceback (most recent call last)
in ()
5 seg = spm.NewSegment()
6 seg.inputs.channel_files = ‘X_train_file_0.nii’
----> 7 seg.run()

~/anaconda3/lib/python3.6/site-packages/nipype/interfaces/base.py in run(self, **inputs)
1079 version=self.version)
1080 try:
-> 1081 runtime = self._run_wrapper(runtime)
1082 outputs = self.aggregate_outputs(runtime)
1083 runtime.endTime = dt.isoformat(dt.utcnow())

~/anaconda3/lib/python3.6/site-packages/nipype/interfaces/base.py in _run_wrapper(self, runtime)
1027 runtime.environ[‘DISPLAY’] = ‘:%d’ % vdisp_num
1028
-> 1029 runtime = self._run_interface(runtime)
1030
1031 if self._redirect_x:

~/anaconda3/lib/python3.6/site-packages/nipype/interfaces/spm/base.py in _run_interface(self, runtime)
321 self.mlab.inputs.script = self._make_matlab_command(
322 deepcopy(self._parse_inputs()))
–> 323 results = self.mlab.run()
324 runtime.returncode = results.runtime.returncode
325 if self.mlab.inputs.uses_mcr:

~/anaconda3/lib/python3.6/site-packages/nipype/interfaces/base.py in run(self, **inputs)
1079 version=self.version)
1080 try:
-> 1081 runtime = self._run_wrapper(runtime)
1082 outputs = self.aggregate_outputs(runtime)
1083 runtime.endTime = dt.isoformat(dt.utcnow())

~/anaconda3/lib/python3.6/site-packages/nipype/interfaces/base.py in _run_wrapper(self, runtime)
1722
1723 def _run_wrapper(self, runtime):
-> 1724 runtime = self._run_interface(runtime)
1725 return runtime
1726

~/anaconda3/lib/python3.6/site-packages/nipype/interfaces/matlab.py in _run_interface(self, runtime)
142 def _run_interface(self, runtime):
143 self.inputs.terminal_output = ‘allatonce’
–> 144 runtime = super(MatlabCommand, self)._run_interface(runtime)
145 try:
146 # Matlab can leave the terminal in a barbbled state

~/anaconda3/lib/python3.6/site-packages/nipype/interfaces/base.py in _run_interface(self, runtime, correct_return_codes)
1748 if not exist_val:
1749 raise IOError(“command ‘%s’ could not be found on host %s” %
-> 1750 (self.cmd.split()[0], runtime.hostname))
1751 setattr(runtime, ‘command_path’, cmd_path)
1752 setattr(runtime, ‘dependencies’, get_dependencies(executable_name,

OSError: command ‘matlab’ could not be found on host Dongqis-MacBook-Pro.local
Interface MatlabCommand failed to run.
Interface NewSegment failed to run.

I have Matlab installed and I can call it with “Matlab” command in Terminal. Btw, I’m using Anaconda3, Matlab_R2017b, spm12 and macOS Sierra.

It will be appreciated if someone can help me. Thanks!

Is the command you type in the terminal exactly “Matlab”? If so could be that nipype is looking for “matlab” (all lowercase) instead. What output do you get from running “which matlab” in terminal?

Either “MATLAB” or “Matlab” or “matlab” will work.
I got “/MATLAB_R2017b.app/bin/matlab” from running “which matlab” in terminal.

@sherrybao it looks like nipype doesn’t have access to your matlab, probably since it’s located in your root directory.
can you try adding the following command after your imports

MatlabCommand.set_default_matlab_cmd('/MATLAB_R2017b.app/bin/matlab')