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.

3
  • 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. Commented Aug 26, 2013 at 2:10
  • What about pipe stdout of the matlab application to python and parse it there? Commented Aug 26, 2013 at 8:43
  • 1
    @BranAlgue: have a look at this approach: stackoverflow.com/a/10964190/97160, it uses the file system to notify Python of an event triggered in MATLAB Commented Aug 26, 2013 at 13:45

2 Answers 2

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.

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

3 Comments

It's an interesting solution. Does this solutions have advantages over file communication?
@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.
Works like a charm but gotta be careful with the number of bytes sent and received, otherwise things get messed up.
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.

4 Comments

No, it doesn't exit. What python library should I use to track text file?
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)
@Raze2dust: there are better ways than busy-waiting to poll for file system changes. See my comment above
@Amro I know, that's what I meant by a watcher above. But the OP asked for the easiest way :)

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.