Error using "set" in a parfor loop
3 views (last 30 days)
Show older comments
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
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?
Answers (1)
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
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.
See Also
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!