Reputation: 1431
with this code:
s=struct([]);
for i=1:5
s(i).m=i;
s(i)
end
it's all ok.. the output is as expected
ans =
m: 1
ans =
m: 2
ans =
m: 3
ans =
m: 4
ans =
m: 5
using a parfor without accessing like this s=struct([]);
parfor i=1:5
s(i).m=i;
end
seems ok..no output but in the workspace I have the right array of struct of 5 elements; but if I try to access to i-th element in the parfor loop like this
s=struct([]);
parfor i=1:5
s(i).m=i;
s(i)
end
I have error on line 4
Index exceeds matrix dimensions.
and in the workspace the s variabile is 0x0 dimension. Why this?
Upvotes: 1
Views: 158
Reputation: 1331
Try this instead..
s=struct([]);
s(5).m = 0;
parfor i=1:5
s(i).m=i;
s(i)
end
Your error occurs because you're trying to change the structure in the parfor loop. So each core is independently trying to add the field m. Another issue is trying to lengthen s dynamically in the parfor loop which is a no-no.
Upvotes: 2