6

I have some computer vision system that works in Matlab. When it detects something I want to tell Python that it found it. Just yes or no without any additional information, but the system works in infinite loop, so I want Python constantly track Matlab somehow.

I wonder what is the easiest way to do this.

For example, Matlab can create a file on a desktop that Python will see and trigger according functionality.

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
Sergey Ivanov
  • 3,719
  • 7
  • 34
  • 59
  • The file thing would be probably the easiest. U could make this file in ramdisk or at least on SSD for faster writing/reading. It depends how often it changes. – Marcin Aug 26 '13 at 02:10
  • What about pipe stdout of the matlab application to python and parse it there? – moooeeeep Aug 26 '13 at 08:43
  • 1
    @BranAlgue: have a look at this approach: http://stackoverflow.com/a/10964190/97160, it uses the file system to notify Python of an event triggered in MATLAB – Amro Aug 26 '13 at 13:45

2 Answers2

7

If you need constant and fast communication I'd suggest you make your Python application listen on a specific port and connect to that port from MATLAB. You can then exchange information in both directions.

Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
  • It's an interesting solution. Does this solutions have advantages over file communication? – Sergey Ivanov Aug 26 '13 at 16:34
  • 1
    @BranAlgue: Principle advantages are speed (no disk involved) and portability (works on all OS, Python and MATLAB can run on different computers). However, it's also more complex to implement, so the optimal approach depends on your particular use case. – Florian Brucker Aug 27 '13 at 09:01
  • Works like a charm but gotta be careful with the number of bytes sent and received, otherwise things get messed up. – karttinen Jan 27 '17 at 11:05
4

Does the Matlab process exit with a particular exit code if it finds something? Just use the exit code in that case. Or else, just make the Matlab process write a file with its output, and then you can create a watcher in python to detect changes in the file.

The simplest way will be to get Matlab to also create an empty file (in addition to the output file itself) when it finds something. Then you can just keep checking if the file exists at regular intervals using os.path.exists() and time.sleep:

import os
import time

path='/path/to/file/created/by/matlab'
while not os.path.exists(path):
    print("Matlab output file still not present. Waiting for 1 s and retrying...")
    time.sleep(1)
print("Matlab process generated output. Now I can do what I want to do")

If you cannot change the matlab script, then you can take a look at mlabwrap, which is a module through which you can call matlab through python. Also see this answer.

Community
  • 1
  • 1
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
  • No, it doesn't exit. What python library should I use to track text file? – Sergey Ivanov Aug 26 '13 at 02:25
  • You can simple have Matlab create an empty file whenever it finds something. In python, just keep checking for that file with some sleep time. (also edited answer to include this) – Hari Menon Aug 26 '13 at 02:46
  • @Raze2dust: there are better ways than busy-waiting to poll for file system changes. See my [comment](http://stackoverflow.com/questions/18435546/communication-between-python-and-matlab#comment27104176_18435546) above – Amro Aug 26 '13 at 13:48
  • @Amro I know, that's what I meant by a watcher above. But the OP asked for the easiest way :) – Hari Menon Aug 26 '13 at 14:14