简体   繁体   中英

Communication between Python and Matlab

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM