How to improve speed when printing multiple images inside a for loop

조회 수: 2 (최근 30일)
Mario A
Mario A 2018년 2월 1일
답변: Rik 2018년 2월 2일
My code generates a figure on each iteration of a for loop, which is printed to a png file each time. Is there a way to speed up this process by letting Matlab continue with the loop while the printing is ongoing? (like a fork-join kind of flow). Any help will be welcome. Example code:
for k=1:10
aux = randn(1e3);
fh = figure('visible','off'); imagesc(aux);
print(fh,'-dpng','-r0','./temp.png'); close(fh)
end
  댓글 수: 2
Rik
Rik 2018년 2월 1일
You are overwriting the image on each loop iteration. And have you tried parfor?
Mario A
Mario A 2018년 2월 1일
The actual code within the for loop (not the example I wrote above) is not compatible with parfor (neither it overwrites the image each time).

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

채택된 답변

Mario A
Mario A 2018년 2월 1일
I just found a solution form the parallel computing toolbox, which is the use of parfeval function. This function allows an asynchronous running of another function. The original example is optimized by writing
for k=1:10
aux = randn(1e3);
fh = figure('visible','off'); imagesc(aux);
parfeval(@print,0,fh,'-dpng','-r0','./temp.png'); close(fh)
end

추가 답변 (1개)

Rik
Rik 2018년 2월 2일
It is also possible print is inherently slower than it could be, in which case the screencapture FEX submission by Yair Altman might result in a speed increase. You can even use that parfeval in combination with this.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by