Repository: nipy/nipype

Issue:
MATLAB:mpath:PathAlterationNotSupported / Modifying the search path is not supported by MATLAB Compiler using MAtlab 2019b
[It seems modifying the matlab path using addpath() is not likely to be supported at any point after MATLAB MCR_R2018b](https://github.com/populse/populse_mia/issues/238).
However, [nipype use addpath()](https://github.com/nipy/nipype/blob/master/nipype/interfaces/matlab.py#L194) command in the matlab script generated .
So [the easy fix is to stick with R2018b.](https://fr.mathworks.com/matlabcentral/answers/554821-compiling-failing-using-matlab-2019b)
But it's a real problem not to be able to use newer MCRs in the future.
Can you remove the addpath() command when generating the script ? Or is there another way to use MCR after R2018b?



Discussion:
I'd like to add that upcoming versions of SPM12 will likely be built with R2021b (MCR v911). If so, downgrading to a previous versions of the MCR might not be possible. The only log term fix is ti get rid of `addpath()`.
Do y'all know what a fix would be? We can obviously remove the line, but we'd need to confirm that it doesn't break workflows.
Keeping it breaks workflows when using recent versions of MCR which is required by Ubuntu 20.04 and later.

I'd simply remove it.
I understand that it is causing forward compatibility problems. Before removing it, we need to understand what problem it was solving and make sure the problem remains solved with the final change.

At worst, we should be able to detect the Matlab version and drop it for more recent versions.
We haven't found any problem it could solve. It looks like `addpath()` had been introduced  by https://github.com/nipy/nipype/commit/078619cab5c4de39ac256f9af462289b50504a09 without any explanation.
The tests we have done on our side seem to indicate that with the MCR, things work correctly by simply commenting out the addpath() command.
In my opinion, when using the MCR, addpath() is unnecessary as the path to spm standalone is known elsewhere.

I think it is useful when using MATLAB as we may want to define where SPM is located.

I suggest as a fix to change[ here](https://github.com/nipy/nipype/blob/master/nipype/interfaces/matlab.py#L194):

`prescript.append("addpath('%s');\n" % path)`
with
`prescript.append("if (isdeployed==false) addpath('%s'); end \n" % path)`

I have not tested this fix but if you wish we can test it with an MCR > R2018b?

However, you are right to test on your side if this does not lead to a regression that we did not think of!
Sorry for the typo!
The effective fix is (with a comma after `end`):
`prescript.append("if (isdeployed==false) addpath('%s'); end,\n" % path)`

I just tested it.

As expected, if we remove the following two lines from [here](https://github.com/nipy/nipype/blob/master/nipype/interfaces/matlab.py#L193):
```
for path in paths :
    prescript.append("addpath('%s');\n" % path)
```
This works well with MCR but not with MATLAB (if SPM was not already in the matlab path).

By using :
```
for path in paths:
    prescript.append("if (isdeployed==false) addpath('%s'); end,\n" % path)
```
everything works fine with MCR and MATLAB (even if SPM was not already in the MATLAB path).

Tested with MCR_R2018b and MATLAB R2020b.


Sounds great to me. Would you like to open a PR?

Resolving patch:
diff --git a/nipype/interfaces/matlab.py b/nipype/interfaces/matlab.py
--- a/nipype/interfaces/matlab.py
+++ b/nipype/interfaces/matlab.py
@@ -191,7 +191,10 @@ def _gen_matlab_command(self, argstr, script_lines):
         else:
             prescript.insert(0, "fprintf(1,'Executing code at %s:\\n',datestr(now));")
         for path in paths:
-            prescript.append("addpath('%s');\n" % path)
+            # addpath() is not available after compliation
+            # https://www.mathworks.com/help/compiler/ismcc.html
+            # https://www.mathworks.com/help/compiler/isdeployed.html
+            prescript.append("if ~(ismcc || isdeployed), addpath('%s'); end;\n" % path)
 
         if not mfile:
             # clean up the code of comments and replace newlines with commas


Test patch: