Break a PAUSE inside a PARFOR loop

8 ビュー (過去 30 日間)
Nex
Nex 2020 年 3 月 11 日
回答済み: Deepak 2024 年 12 月 2 日
I have a parfor loop that executes two functions. One is an indefinite pause, the other is an infinite loop:
parfor i = 1:2
if i == 1
%Start hardware A data acquisition
pause()
%Close hardware A data acqusition
else
while true %task continues until manually stopped
%USER INPUT BREAK HERE (how?)
%Hardware B data acqusition
end
end
end
I want a break function within the second parfor to also break the pause in the first parfor. Is that possible?
Also, how can messages be displayed from within the parfor loop? GUI butons, DISP and FPRINTF don't seem to work.
Obs: "Hardware A" (Tobii Eye Tracker) SDK only collects data during a "pause", so the pause and the parfor are necessary.

回答 (1 件)

Deepak
Deepak 2024 年 12 月 2 日
Hi Nex,
To manage parallel data acquisition from two hardware devices in MATLAB, a “parfor” loop runs two tasks concurrently: Task 1 maintains "Hardware A" data acquisition using an indefinite pause, while Task 2 continuously acquires data from "Hardware B" in an infinite loop until a user-triggered event.
Communication between tasks can be achieved using “parallel.pool.DataQueue” in MATLAB, allowing Task 2 to send a stop signal to break the pause in Task 1, effectively halting both tasks. Messages from within the “parfor” loop are managed through the “DataQueue”, ensuring they are displayed in a controlled manner.
Here is the sample MATLAB code to achieve the same:
% Create a DataQueue to communicate between workers
dq = parallel.pool.DataQueue;
% Function to handle stop signal
function handleStopSignal(~)
disp('Stop signal received');
% You might use a more complex logic here to stop your hardware
end
% Set up the listener to handle messages from the DataQueue
afterEach(dq, @handleStopSignal);
parfor i = 1:2
if i == 1
% Start hardware A data acquisition
fprintf('Hardware A started\n');
pause(); % This will pause indefinitely
% Close hardware A data acquisition
fprintf('Hardware A stopped\n');
else
while true
% Hardware B data acquisition
fprintf('Hardware B acquiring data\n');
% Simulate user input break
% In real scenario, replace this with a GUI button or condition
if rand() > 0.99 % Random condition to simulate stop
send(dq, true); % Send stop signal
break;
end
% Pause for a short duration to prevent excessive CPU usage
pause(0.1);
end
end
end
Please find attached the documentation of functions used for reference:
I hope this assists in resolving the issue.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by