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.dataans =11:36:56.387ans =11:36:56.387% ... snip
However, when the parallel pool is open, the data is not updated correctly; e.g. (after processing):
exArray.dataans = []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.
Best Answer