accumulate values in aftereach function

조회 수: 8 (최근 30일)
dleal
dleal 2022년 4월 19일
댓글: dleal 2022년 4월 20일
Hi all,
I would like to accumulate values that I collect in real time from a function that runs in the background.
For example
q = parallel.pool.DataQueue;
f1 = AfterEach(q, @afterFunc);
fut1 = parfeval(@genData, 0, q1)
function genData(q1)
while true
send(q1, randn)
end
end
Ideally I would be able to accumulate using persistent variable, but I understand persistant variables cannot be used in cunjunction with parfeval:
function afterfunc(x)
persistent y;
if isempty(y); y = []; end;
y = [ y, x];
plot(y); % for example
end
I know I could accumulate the values in the genData(q1) function, but then, after a few thousand iteration, I would be sending a very large vector and it would take longer (then desired) to send

채택된 답변

Raymond Norris
Raymond Norris 2022년 4월 19일
Persistent variables should work. Try the following
q = parallel.pool.DataQueue;
q.afterEach(@afterFunc);
f = parfeval(@genData, 0, q);
function genData(q)
while true
send(q, randn)
pause(1)
end
end
function afterFunc(x)
persistent y
if isempty(y)
y = [];
end
y = [y, x];
plot(y)
end
  댓글 수: 1
dleal
dleal 2022년 4월 20일
Hi Raymond, thanks for your answer. This is exactly what I needed.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Asynchronous Parallel Programming에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by