필터 지우기
필터 지우기

Error when plotting in parfor loop

조회 수: 23 (최근 30일)
David Perlmutter
David Perlmutter 2018년 12월 19일
댓글: Walter Roberson 2021년 9월 3일
I'd like to plot inside a parfor loop, but am getting an error I don't understand.
parfor i=1:10
figure;
end
Cannot set WindowStyle to 'docked' when MATLAB is
started with no display or when the -noFigureWindows
option is specified.
I have changed my default window style but got the same error
set(0,'defaultfigurewindowstyle','normal')
set(0,'defaultfigurewindowstyle','docked')
  댓글 수: 1
Mark Sherstan
Mark Sherstan 2018년 12월 19일
Are you running any other code before the loop? What version of MATLAB and parrallel toolbox are your using?

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

채택된 답변

David Perlmutter
David Perlmutter 2018년 12월 19일
I get the following
parfor i=1:5
figure(i)
plot(rand(i*10,1));
saveas(gcf,['temp' num2str(i) '.jpg']);
end
Cannot set WindowStyle to 'docked' when MATLAB is started with no display or
when the -noFigureWindows option is specified.
Maybe I have some default settings that are preventing graphics objects from opening in my parallel workers?
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 12월 19일
It does sound like you have set(0,'defaultfigurewindowstyle','docked') in effect. Check for a startup.m that might be setting it.

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

추가 답변 (5개)

Walter Roberson
Walter Roberson 2018년 12월 19일
It is never possible to plot to the display inside any parallel construct. The workers are in separate processes that do not have access to the graphics subsystem.
You would need to either:
  1. Calculate all of the data in the parfor and then do the plotting after the parfor; or
  2. Use parallel.pool.DataQueue or parallel.pool.PollableDataQueue and send() data back to the client to do the plotting such as by using foreach

David Perlmutter
David Perlmutter 2018년 12월 19일
No other code. The error occurs just after opening a new matlab instance.
I'm using Parallel Computing Toolbox Version 6.11 (R2017b)

Mark Sherstan
Mark Sherstan 2018년 12월 19일
편집: Mark Sherstan 2018년 12월 19일
To save a figure in a parfor loop you must do the following as found here. Otherwise save the data and plot after as mentioned by Walter.
parfor i=1:5
figure(i)
plot(rand(i*10,1));
saveas(gcf,['temp' num2str(i) '.jpg']);
end
Try the code above and let us know if the error continues.

David Perlmutter
David Perlmutter 2018년 12월 19일
According to this, it should be possible for parallel workers to plot:
I don't mind my plots being invisible, but I would like to save them to view later.
Note: I get the same error if I use the "plot" command instead of the "figure"command
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 12월 19일
Note that the plots are not just "invisible": they are in a different process. You would need to save them or else find a way to bring back the figures.

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


David Perlmutter
David Perlmutter 2018년 12월 20일
Turns out startup.m was the problem!
I had several default plotting settings, including set(0,'defaultfigurewindowstyle','docked'), in my startup.m
When I comment them all out, I am able to create plots on parallel workers without error.
Thanks Walter and Mark!
  댓글 수: 2
Jackson Jewett
Jackson Jewett 2021년 9월 3일
Could you clarify this? Are you able to display plots within a forloop? Could you advise what you commented out in order to see these results? Thank you!
Walter Roberson
Walter Roberson 2021년 9월 3일
You can save plots inside of parfor; you just cannot display them to the user from inside of the parfor.

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

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by