Recently, I try to call matlab R2022a in 3D Slicer 5.1.0, but failed!
when “import matlab.engine”, 3D Slicer crashed.
The problem may occur in line 8 in the file “enginesession.py” in the matlabengine for python packages, when calling pythonengine.createProcess().
Matlab engine may use different version of some shared libraries than those in Slicer’s Python environment, therefore you may need to put the script that relies on Matlab into a separate .py file and launch it as an external process.
You may also try to use Matlab engine from a Python CLI module - see example here.
It’s a good suggestion. However, Matlab (R2019) engine can be imported successfully in Slicer ( version 4.11 ). There must be changes in Slicer v5. Is there any difference between PythonSlicer and the python integrated in Slicer?
One problem of using CLI is that if the size of the volume data is very large, it will take a long time for data exchange between CLI and Slicer. For example, Slicer should save the data into a temporary file and CLI will read the data from the temporary file, after the CLI execution, CLI had to save the result into a temporary file and Slicer had to read it from the temporary file. Anyway, there is no other options.
If you don’t compress the volume then writing to disk and reading from disk should take negligible time. If you have enough memory then most likely the file will be transferred via memory, so disk speed should not even matter.
You may also reconsider using Matlab. Licensing hassles, difficulty of debugging a closed-source software, complications with distributing your final software might not worth the trouble of using Matlab. You can probably find free, open-source Python modules that can do the same as the proprietary Matlab functions.