Handle object array doesn't update when parfor is used within a class method
古いコメントを表示
I have a class that is similar to this:
classdef Example < matlab.mixin.Copyable
properties
data = [];
end
methods
function obj = Example()
end
function process(obj)
parfor ii = 1:numel(obj)
objSlice = obj(ii);
objSlice.process_sub()
obj(ii) = objSlice;
end
end
function process_sub(obj)
obj.data = datestr(now, 'HH:MM:SS.FFF');
end
end
end
As you can probably see, I would like to process the elements of an array of this class in parallel. This works as expected when the parallel pool is closed, e.g.:
% Initialise array of objects
num = 5;
for nn = 1:num
exArray(nn) = Example();
end
% Process the object array in parallel
exArray.process();
exArray.data
ans =
11:36:56.387
ans =
11:36:56.387
% ... snip
However, when the parallel pool is open, the data is not updated correctly; e.g. (after processing):
exArray.data
ans =
[]
ans =
[]
% ... snip
Is this behaviour expected or a bug? And either way, how can I get the object array to process in parallel?
I have seen other similar questions (e.g. here , here), and the documentation ( here ), but I think my issue is different because the parfor loop is actually inside the handle object, and I haven't been able to find any ideas about this.
Thanks very much in advance for your help.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!