Last active
April 24, 2018 09:17
-
-
Save GeorgySk/c76c6c4d9fd635d54d60a40c1de0ccb8 to your computer and use it in GitHub Desktop.
Example of how to call MATLAB function from Python and get output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
For installation see: | |
https://uk.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html | |
In order to install matlab.engine in non-default location, | |
e.g. conda environment, see: | |
https://uk.mathworks.com/help/matlab/matlab_external/install-matlab-engine-api-for-python-in-nondefault-locations.html | |
(My advice is to install it to a base environment, | |
installing to different environments seems to be buggy) | |
If this doesn't work, try `pip install -e .` from *matlabroot\extern\engines\python* | |
""" | |
import io | |
import matlab.engine | |
eng = matlab.engine.start_matlab() | |
out = io.StringIO() | |
err = io.StringIO() | |
# .m file with the function should be in the same folder | |
result = eng.some_matlab_function(some_argument, | |
stdout=out, | |
stderr=err) | |
print(out.getvalue()) # will print whatever is `display`ed in MATLAB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment