{"id":18076255,"url":"https://github.com/scivision/matlab-engine-python","last_synced_at":"2025-04-05T19:42:57.053Z","repository":{"id":80567081,"uuid":"504641128","full_name":"scivision/matlab-engine-python","owner":"scivision","description":"Examples: Matlab Engine from Python and Matlab calling Python","archived":false,"fork":false,"pushed_at":"2025-01-22T16:48:19.000Z","size":59,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-11T17:59:43.338Z","etag":null,"topics":["matlab-engine","matlab-python-interface"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scivision.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-17T18:51:08.000Z","updated_at":"2025-01-22T16:48:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"8b37c51a-053e-4b35-860e-571a52966c95","html_url":"https://github.com/scivision/matlab-engine-python","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fmatlab-engine-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fmatlab-engine-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fmatlab-engine-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fmatlab-engine-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scivision","download_url":"https://codeload.github.com/scivision/matlab-engine-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393538,"owners_count":20931809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["matlab-engine","matlab-python-interface"],"created_at":"2024-10-31T11:09:24.146Z","updated_at":"2025-04-05T19:42:57.032Z","avatar_url":"https://github.com/scivision.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Matlab Engine for Python\n\nThis repo gives examples of calling Matlab functions from Python using\n[Matlab Engine for Python](https://www.mathworks.com/help/matlab/apiref/matlab.engine.matlabengine.html)\nand also examples of Matlab Interface to Python.\nCompatible\n[versions of Python and Matlab](https://www.mathworks.com/support/requirements/python-compatibility.html)\nare required for either interface.\n\nInstall necessary packages for these examples:\n\n```sh\nconda install --file requirements.txt\n\n# OR\npython -m pip install -r requirements.txt\n```\n\nPython can directly exchange data with Matlab and call Matlab functions via Matlab Engine for Python.\n[Setup](https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html)\nMatlab Engine for Python:\n\n```sh\npython -m pip install matlabengine\n```\n\nOr install directly from Matlab directory like:\n\n```sh\npython -m pip install $(matlab -batch \"disp(matlabroot)\" | tail -n1)/extern/engines/python/\n```\n\n[Article](https://www.scivision.dev/matlab-engine-python-install/)\nexplains more details of Matlab Engine for Python.\n\n## Numpy.ndarray return from Matlab\n\nMatlab Engine accepts Numpy.ndarray into Matlab functions.\nGetting Numpy.ndarray from Matlab Engine returned arrays requires like:\n\n```python\nimport numpy\nimport matlab.engine\neng = matlab.engine.start_matlab(\"-nojvm\")\n\nx = numpy.random.random((3,2))\n\ny_m = eng.my_matlab_fun(x)\n\ny = numpy.array(y_m.tomemoryview()).reshape(y_m.size, order=\"F\")\n```\n\n## Matlab functions with more than one output\n\nFor Matlab functions that return more than one output, use \"nargout=\" like:\n\n```python\nimport matlab.engine\neng = matlab.engine.start_matlab(\"-nojvm\")\n\nx, y = eng.myfun(x, nargout=2)\n```\n\n## Data file exchange\n\nThe Python [pickle](https://docs.python.org/3/library/pickle.html) module can save and load data to/from files.\nWhile standard file formats such as HDF5 should be used instead of pickle, Matlab can read / write Pickle data using the Matlab Interface to Python.\n\n[Example of write / read Python pickle data from Matlab](./PythonPickle.m).\n\n\n### HDF5\n\nTo avoid using Matlab Engine, which requires compatible versions of Python and Matlab,\none can interchange data using a file and calling the other language interpreter.\n\nExample: [image_hdf5.py](./image_hdf5.py) calls Matlab function [image_hdf5.m](./image_hdf5.m).\n\nMatlab Engine example: [image_matlab_engine.py](./image_matlab_engine.py).\n\n## Matlab using Python\n\nMatlab can use Python code via\n[pyenv](https://www.mathworks.com/help/matlab/ref/pyenv.html).\n\nExample: [image_proc.m](./image_proc.m).\n\n## Troubleshooting\n\n### Numpy import issues\n\n[check_numpy.m](https://github.com/scivision/matlab-interface-to-python/blob/main/check_numpy.m)\nshows how to check issues with DLLs on Windows with Numpy and Matlab.\n\nPython\n[importlib](https://docs.python.org/3/library/importlib.html)\ncan also give hints about problems importing:\n\n```matlab\npy.importlib.import_module('numpy')\n```\n\nOther things to try especially on Windows include:\n\n```matlab\nsetenv(\"KMP_DUPLICATE_LIB_OK\", \"TRUE\")\n```\n\nthis avoids Matlab crash below--is this error from Matlab libiomp5md.dll loaded and Numpy also loads that DLL?\n\n```\nOMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Fmatlab-engine-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscivision%2Fmatlab-engine-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Fmatlab-engine-python/lists"}