Calling python scripts from Matlab and go to the next line without waiting
25 次查看(过去 30 天)
显示 更早的评论
Hi all, I have a python script which handle a motor. The motor has a position feedback, so the python script waits for the position to be reached, before quitting. I'm using Maltab in a big app, and I want to move the motor while some cameras are showing a preview in matlab. I'm trying by uysing pyrunfile,which is great, but the problem is that the app waits for the script to complete before moving to the next line. It is worth noting that the script does not provide any output. So now this is what i get:
- start camera preview and see the live images
- start the python script, the motor moves
- the preview freezes untill the motor stops, then start again
while i would like to have
- start camera preview and see the live images
- start the python script, the motor start moving
- the preview shows live images while the motor is moving
Can anyone help me? Is there a way to run the python script from matlab, forcing matlab not to wait for the script completion?
0 个评论
回答(1 个)
Suraj
2023-3-29
Hi Paolo
It is my understanding that you want to run your python script in a non-blocking fashion. You can achieve this by performing your activity on a separate thread that runs in the background. Here’s an example of this –
script.py
from threading import Thread
def my_function():
# Code that takes time to run
Thread(target = my_function).start()
print("This is printed and the python interpreter returns control to MATLAB")
MATLAB Code
pyrunfile('script.py')
Here, the function 'my_function()' runs in the background and 'pyrunfile' returns immediately without blocking operations. This should allow your motor to move without freezing the preview.
Hope this helps.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!