MATLAB: Disp in background thread

parallel computing

Is it possible to disp some text to main console while executing code in background? I have an infinite function that is started by parfeval but it's difficult to track what's going on there and simply putting disp in code shows nothing (I guess that's beceause parallel worker has its own console output).

Best Answer

You could use parallel.pool.DataQueue together with afterEach to achieve this.
q = parallel.pool.DataQueue();
afterEach(q, @disp);
f = parfeval(@myFcn, 0, q);
function myFcn(q)
for idx = 1:100
pause(3);
send(q, sprintf('Iteration: %d at time: %s', idx, string(datetime)));
end
end