The relationship between Python and Matlab

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

Interestingly, this is the easiest way to do this.

For example, Matlab can create a file on the desktop that Python sees and activates in functionality.

+6
source share
2 answers

If you need a constant and fast connection, I suggest you make the Python application listen on a specific port and connect to that port from MATLAB . Then you can exchange information in both directions.

+7
source

Is the Matlab process running with a specific exit code if it finds something? Just use the exit code in this case. Or just start the Matlab process, write a file with its output, and then you can create an observer in python to detect changes in the file.

The easiest way is to force Matlab to also create an empty file (in addition to the output file itself) when it finds something. Then you can simply check 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, you can take a look at mlabwrap , which is a module through which you can call matlab through python. Also see this answer .

+4
source

Source: https://habr.com/ru/post/952472/


All Articles