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!