Calling python scripts from Matlab and go to the next line without waiting
40 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Suraj
on 29 Mar 2023
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.
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!