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.

조회 수: 4 (최근 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
Here is a more complete runnable example
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.

채택된 답변

Brad
Brad 2016년 10월 5일
The problem is solved now using a timer method. If you're interested, I left notes in the comments section here about how it works.

추가 답변 (1개)

Joe Yeh
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
Brad
Brad 2016년 10월 5일
The above code is just an example of the issue. I'm aware that the code does not break from the while loop. I'm looking for an example of code that can run a while-loop and a for-loop simultaneously; where something can happen inside the for-loop that causes the while-loop to break or update its information.
Karthik Sampathkumar
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 CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by