Minimal example where you run two loops simultaneously, and programmatically stop/switch a for-loop or while-loop or update the information inside those loops, while they are executing.
조회 수: 10 (최근 30일)
이전 댓글 표시
I have a question related to parallel computing, how to run two loops simultaneously, and how to programmatically stop a for loop or update the information inside a while loop.
I'm writing a program that performs animal tracking (a rat/mouse) in an open field box, and sends pulse (voltage) data to a DAQ device based on the rat's current location. This is for optogenetics experiments.
The image acquisition and tracking code is done.
The pulse generation code is done.
My problem is that I cannot do both simultaneously.
The DAQ is a basic device that takes a single voltage value between 0.0 - 5.0 range. Generating a pulse requires constructing an array of voltage values and looping over them. A simple example...
for v = 0 : .1 : 5
DAQsend(v)
end
Tracking the rat around an open field requires capturing images from a camera, locating the subject, and determining whether it's position is inside some ROI. This also requires a loop...
for t = 1:1000
im = getCamImage()
roi = determineROI(im)
end
As you can see both the tracking and the DAQ pulse generation require loops, where the DAQ loop would be nested inside the tracking loop...
for t = 1:1000
im = getCamImage()
roi = determineROI(im)
if roi == 1
v = 1;
while t < 1000
DAQsend(voltageArray(v))
v = v+1;
end
else
DAQsend(0)
end
end
Unfortunately, this code does not allow me to continue generating pulses while updating the tracking information. I'm wondering if something like this is even possible...
for t = 1:1000
im = rand;
if im > .99
v = 1;
while t < 1000
disp(v)
v = v+1;
pause(.1)
end
end
end
So far I've been able to use the batch function in the parallel computing toolbox to run two loops simultaneously.
daqjob = batch('DAQsend',1,{daqObject,voltageArray});
This allows me to perform animal tracking and generate a single pulse train on a parallel worker. However, I cannot update or stop this pulse train.
I would be appreciative and in awe of anyone could provide a minimal example where you run two loops simultaneously, and programmatically stop/switch a for-loop or while-loop or update the information inside those loops, while they are executing.
채택된 답변
추가 답변 (1개)
Joe Yeh
2016년 10월 5일
for t = 1:1000
im = getCamImage()
roi = determineROI(im)
if roi == 1
v = 1;
while t < 1000
DAQsend(voltageArray(v))
v = v+1;
end
else
DAQsend(0)
end
end
The above code doesn't work as you wanted it to because the program can't break from the while loop. When the program enters the while loop, the loop condition (t<1000) is always satisfied because it's never updated. Correcting the loop condition should solve this problem.
for t = 1:1000
im = getCamImage()
roi = determineROI(im)
if roi == 1
v = 1;
while v < 1000 % Send out 999 pulses
DAQsend(voltageArray(v))
v = v+1;
end
else
DAQsend(0)
end
end
댓글 수: 2
Karthik Sampathkumar
2019년 2월 26일
hi Brad,
I require a similar solution. Have you found the solution for the problem, if yes could you share your ideas please.
Thank you
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!