Giter VIP home page Giter VIP logo

sharedmatlabengine.jl's Introduction

SharedMATLABEngine.jl

NOTICE:

This seems to be broken on newer versions of MATLAB due to some changes in the Python API. Or something. I don't know. If you have any ideas for how to fix it, let me know. I also don't have a recent MATLAB license, so it's a little tough to debug stuff.

SharedMATLABEngine allows Julia to share data with an open MATLAB session through a MATLAB command line embedded in the Julia REPL. The functionality is similar to MATLAB.jl. The main advantage of SharedMATLABEngine.jl over MATLAB.jl is the ability to connect to an already open named MATLAB engine session. The advantages of MATLAB.jl over SharedMATLABEngine.jl are... well a lot. A lot more effort has gone into that project than this one. Unless you really need to connect to an already open MATLAB session, you should probably use MATLAB.jl.

SharedMATLABEngine goes through a PyCall and the uses the matlab.engine interface in Python.

Installation

Since SharedMATLABEngine connects through the Python API for the MATLAB Engine, Python must first be installed.

Once Python is installed, the MATLAB Engine API for Python needs to be set up. In the MATLAB command prompt, enter:

>> cd (fullfile(matlabroot,'extern','engines','python'))

>> system('python setup.py install')

Finally, SharedMATLABEngine must be installed in Julia. To do so, press the ] key in the Julia REPL to enter Pkg mode and enter:

(@v1.x) pkg> add SharedMATLABEngine

From here, SharedMATLABEngine should work. If not, check out the documentation for installing the MATLAB Engine API in Python.

Connecting to a MATLAB Session

Sharing the MATLAB Engine

To begin using SharedMATLABEngine, open up a MATLAB session and enter the following in the Command Window:

>> matlab.engine.shareEngine

We can now see the name of the shared engine

>> matlab.engine.engineName

ans =

    'MATLAB_77164'

Connecting Julia to the MATLAB Engine

To connect the Julia REPL to the MATLAB session, enter:

julia> using SharedMATLABEngine

julia> connect_matlab()

Once connected, you will see the message:

REPL mode SharedMATLABEngine initialized. Press > to enter and backspace to exit.

If multiple shared MATLAB Engine sessions are open, you can specify the one you want to connect to as a string argument such as connect_matlab("MATLAB_77164"). To see a list of available MATLAB sessions, call find_matlab().

General Use

Now that we are connected, the MATLAB command line can be accessed from the Julia REPL by typing >

>> a = magic(3)

a =

     8     1     6
     3     5     7
     4     9     2

Julia variables and commands can be interpolated into MATLAB commands via the $ operator.

julia> n = 1
1

>> $a21 = a($(n+1), 1)
3.0

>> b = {zeros($a21), 'some words'}

b =

  1×2 cell array

    {3×3 double}    {'some words'}


>> [$z, c] = b{:}

c =

    'some words'

julia> (a21, z)
(3.0, [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])

The mat" string macro can also be used to call MATLAB commands without switching the Julia REPL to MATLAB mode

julia> b = a21 .+ mat"a+1"
3×3 Matrix{Float64}:
 12.0   5.0  10.0
  7.0   9.0  11.0
  8.0  13.0   6.0

Variables from the MATLAB workspace can be accessed through the exported matlab_workspace variable.

julia> matlab_workspace.a
3×3 Matrix{Float64}:
 8.0  1.0  6.0
 3.0  5.0  7.0
 4.0  9.0  2.0

Tips

Defualt Sharing

To make MATLAB share sessions by default, open your startup file for editing with

>> edit(fullfile(userpath,'startup.m'))

paste the following lines

try
    matlab.engine.shareEngine;
catch e
    warning(['Cannot share MATLAB session.  ' e.message])
end

and save the file.

Attributions

Icon modified from car-engine made by Freepik from www.flaticon.com

sharedmatlabengine.jl's People

Contributors

github-actions[bot] avatar jonniedie avatar kristofferc avatar pedrominicz avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

kristofferc

sharedmatlabengine.jl's Issues

i'm getting this. what might be my problem?

I have been trying to implement this code.
I have installed Matlab 2019b which was downgrade to Matlab 2020a due to compatibility of MATLAB engine API.
I have installed python 3.9 and pycharm. the environment set to MATLAB home.
but I'm still receiving the error message.

I deleted the package in Julia and reinstalled again.
i added "sys.path.append("matlab19bPy36")" and still get the same error message.

what might be my problem?
can you help me?

Error from Julia
InitError: PyError ($(Expr(:escape, :(ccall(#= C:\Users\mitadm.julia\packages\PyCall\BcTLp\src\pyeval.jl:38 =# @pysym(:PyEval_EvalCode), PyPtr, (PyPtr, PyPtr, PyPtr), o, globals, locals))))) <class 'ModuleNotFoundError'>
ModuleNotFoundError("No module named 'matlab'")
File "C:\Users\mitadm.julia\packages\PyCall\BcTLp\src\pyeval.jl", line 1, in
const Py_single_input = 256 # from Python.h

during initialization of module SharedMATLABEngine
in _handle_error at PyCall\BcTLp\src\exception.jl:83
in pyerr_check at PyCall\BcTLp\src\exception.jl:66
in pyerr_check at PyCall\BcTLp\src\exception.jl:62

Error from python
Traceback (most recent call last):
File "C:\work\matlab19bPy37\Lib\site-packages\matlab\engine_init_.py", line 45, in
pythonengine = importlib.import_module("matlabengineforpython"+PYTHONVERSION)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\importlib_init
.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'matlabengineforpython3_7'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\work\matlab19bPy37\Lib\site-packages\matlab\engine_init_.py", line 61, in
pythonengine = importlib.import_module("matlabengineforpython"+PYTHONVERSION)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\importlib_init
.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 670, in _load_unlocked
File "", line 583, in module_from_spec
File "", line 1043, in create_module
File "", line 219, in _call_with_frames_removed
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/mitadm/PycharmProjects/pythonProject/main.py", line 25, in
import matlab.engine
File "C:\work\matlab19bPy37\Lib\site-packages\matlab\engine_init_.py", line 64, in
'MathWorks Technical Support for assistance: %s' % e)
OSError: Please reinstall MATLAB Engine for Python or contact MathWorks Technical Support for assistance: DLL load failed: The specified module could not be found.

Process finished with exit code 1

Multi-line expressions

This doesn't work:

mat"""
temp = 1:10
for ii = 1:10
    temp(ii) = rand()
end
"""

Also, at the REPL it would be nice to have the same continuation rules that the MATLAB command prompt has for for loops and ... and such.

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.