Error using "set" in a parfor loop

3 views (last 30 days)
Anthony
Anthony on 22 Dec 2014
Commented: Edric Ellis on 23 Dec 2014
Hi all,
I am trying to use a parfor loop to run an optimization on several different images in parallel to save time. To make sure the workers are being passed the correct figure, I create an array of figure handles outside the parfor loop, and then inside the loop I query the worker ID number with
task=getCurrentTask;
worker=task.ID;
then I use the number stored in worker to pass in the correct figure handle from the array of figure handles to the set command to make that figure handle the current figure.
fig=figHandles(worker);
set(0,'currentfigure',fig);
This works when I am not using a for loop, but when I change it to a parfor loop, I get an error using the set command saying it is an invalid object handle.
Can anyone explain this or tell me how to fix it?
  1 Comment
Edric Ellis
Edric Ellis on 23 Dec 2014
Remember that MATLAB workers are completely separate MATLAB processes. As such, they do not have access to figures created on other workers, or on the client. Could you post a minimal reproduction demonstrating the error occurring with PARFOR?

Sign in to comment.

Answers (1)

Jan
Jan on 22 Dec 2014
When the treads are working in parallel, they cannot set the CurrentFigure porperty. Of course this must provoke collisions. The error message is not clear then, but what do you expect when multiple threads set the CurrentFigure simultaneously?
Another idea is that you are using Matlab 2014b and 0 is not the root object anymore.
  2 Comments
Anthony
Anthony on 22 Dec 2014
I am using 2013A. I admit that I don't know all the details of how a parfor loop works with regard to handling figure windows. Let me clarify my problem.
I am running an iterative optimization on several images at a time. I want to make sure that each worker plots to the same figure every time, (each worker has its own figure) and I am not sure how the parfor deals with keeping track of which figure is current, so I have the method of keeping track of that as I previously described.
Am I correct in assuming that each worker has it's own workspace, and that if I create a figure within the parfor loop, that the individual worker will only work on that figure, since it is the only one in its workspace? If that is true I believe that might solve my problem.
Thanks!
Jan
Jan on 22 Dec 2014
As far as I know, the active figure is not determined in the workspace. But you can create a figure in each worker and draw to it by specifying its handle:
FigH = figure;
AxesH = axes;
plot(1:10, rand(1, 10), 'Parent', AxesH);
Now the workers do not draw to the CurrentFigure, which must cause collisions, but to their locally defined figure.
Please try if this is working. I did not test this.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!