multiple cprintf in parallel loop problem

I want to print a secetence with different color in the command windows using 'cprintf':
https://www.mathworks.com/matlabcentral/fileexchange/24093-cprintf-display-formatted-colored-text-in-the-command-window
below are the test code:
q = parallel.pool.DataQueue;
afterEach(q, @(args) cprintf(args{:}));
parfor i=1:1:100
tic
pause(0.5)
time1=toc;
fprintf(', Reading time:'); send(q,{'Keywords','%4.1f',time1}); fprintf(' seconds,')
end
the code runs OK using regular for-loop, but once I changed it into parfor, the printed result are chaos, this may be caused by the combination of fprintf-cprintf-fprintf.
is there anyway to solve this problem?
Thanks!
Yu

 채택된 답변

Edric Ellis
Edric Ellis 2018년 9월 19일

0 개 추천

To stop the output being interleaved, you need to send all the stuff to be printed as a single message. Here's one way:
q = parallel.pool.DataQueue;
afterEach(q, @multiCprintf);
parfor i=1:1:100
tic
pause(0.5)
time1=toc;
% Send multiple sets of things to print in a cell-of-cells
send(q, {...
{'Reading time: '}, ...
{'Keywords', '%4.1f', time1}, ...
{' seconds\n'}});
end
function multiCprintf(argsCell)
% loop over the outer layer of cells, and call cprintf on
% the inner layers.
for idx = 1:numel(argsCell)
theseArgs = argsCell{idx};
cprintf(theseArgs{:});
end
end

댓글 수: 4

Yu Li
Yu Li 2018년 9월 19일
Thanks for your answer.
Really fantastic, I've never seen writing parfor like this before.
Thank you~
Yu
Yu Li
Yu Li 2018년 9월 19일
편집: Walter Roberson 2018년 9월 19일
Hi:
sorry to bother you, I met a further question regarding this topic, just a simple question but I do not know if there is any mistake on my code.
below is the test code:
warning off all
q = parallel.pool.DataQueue;
afterEach(q, @multiCprintf);
unit='Second';
send(q,{{'timing: '},{'Keywords','%4.2f',100},{' Seconds\n'}})
send(q,{{'timing: '},{'Keywords','%4.2f',100},{' %s\n',unit}})
function multiCprintf(argsCell)
% loop over the outer layer of cells, and call cprintf on
% the inner layers.
for idx = 1:numel(argsCell)
theseArgs = argsCell{idx};
cprintf(theseArgs{:});
end
end
there are two 'send' in this code, where the unit='Second'. but if you run it, you may see that when I replaced the 'Seconds' with ''%s',unit', the 'Second' is not printed.
I'm not sure if there is any mistake with my code, could you please give me some suggestions?
Thanks!
Yu
I think this is because cprintf has 2 different syntaxes:
1. cprintf(style, format, ...)
2. cprintf(literalText)
In particular, there's no cprintf(format, ...).
So, when you do
cprintf(' %s\n', 'Second')
cprintf is actually throwing an error because ' %s\n' isn't a valid style. You can fix this by using
{'Text', ' %s\n', unit}
which tells cprintf to print in the plain style.
Yu Li
Yu Li 2018년 9월 20일
Thank you!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MuPAD에 대해 자세히 알아보기

질문:

2018년 9월 18일

댓글:

2018년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by