4

I'm working on system which written mostly in Matlab and partially in Python. Python scripts are called from Matlab. I'm looking for convenient way to debug Python code, called from Matlab, with some IDE (like PyCharm).

I'll be glad to receive advice if it is possible and how. Windows 10, Matlab R2018b, Python 3.6

5
  • PyCharm is good but I can't imagine what it might mean to open it with Matlab Commented May 10, 2020 at 9:06
  • @SardarUsama: The question is if any IDE is actually able to debug python code by MATLAB. For C/C++ code called form MATLAB we can use MSVC "attach to process", no clue if Python has some similar concepts which actually work here. Commented May 10, 2020 at 9:58
  • Are you on Windows? Commented May 10, 2020 at 10:32
  • @Daniel PyCharm has an ability to attach to process. But how can I know to which process to attach ? Commented May 10, 2020 at 11:03
  • @Paolo Yes, Windows 10 Commented May 10, 2020 at 11:05

1 Answer 1

5

Here's a solution to debug your Python code, executed from MATLAB, in Microsoft Visual Studio.

This solution was tested on Windows 10, with Python 3.6, MATLAB R2020a and Visual Studio 2015.


Create your .py file:

# mymod.py
"""Python module demonstrates passing MATLAB types to Python functions"""
def search(words):
    """Return list of words containing 'son'"""
    newlist = [w for w in words if 'son' in w]
    return newlist


def theend(words):
    """Append 'The End' to list of words"""
    words.append('The End')
    return words

In MATLAB, import the module and create a dummy list:

>> mod = py.importlib.import_module('mymod');
>> N = py.list({'Jones','Johnson','James'});

Open Visual Studio and create a new Python project from existing code. Then, select Attach to Process from the Debug menu:

enter image description here

Search for MATLAB:

enter image description here

select the MATLAB process and attach.

Place a breakpoint in your code:

enter image description here

Now go back to MATLAB and invoke the function:

>> py.mymod.search(N)

MATLAB command window will halt. Go to Visual Studio and debug your code:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. The same procedure works also with PyCharm !
@ivbsd1 what Matlab and PyCharm versions are you using? I've tried the exact same procedure but it fails to respect breakpoints.
Pycharm 2019.2.4, Matlab 2019b. It does not work with PyCharm 2020

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.