Problems with parfor in class method
5 次查看(过去 30 天)
显示 更早的评论
If I run
F=Foo(1);
F.A=pi;
F.bar();
where
classdef Foo
properties
A;
sz;
end
methods
function obj = Foo(sz)
obj.sz=sz;
end
function obj=set.A(obj,A)
if size(A)==obj.sz
obj.A=reshape(A,numel(A),[]);
end
end
function []=bar(obj)
parfor i=1:1
obj.A(1);
end
end
end
end
then I get
Error using Foo/bar (line 16)
Index exceeds matrix dimensions.
Error in test (line 4)
F.bar();
The error disappears when I replace the parfor-loop with a regular for-loop.
I get a warning in my editor on the line "if size(A)==obj.sz", saying "A set method for a non-dependent property should not access another property". However, if I make the variable "A" dependent, then I can't define a set method.
EDIT: I found out that the error also disappears when I declare "sz" first, and then "A".
0 个评论
采纳的回答
Matt J
2016-4-17
编辑:Matt J
2016-4-17
Very strange indeed. However, the following seems to solve all problems.
classdef Foo
properties
Adata;
sz;
end
properties (Dependent)
A;
end
methods
function obj = myclass(sz)
obj.sz=sz;
end
function obj=set.A(obj,val)
if isequal(size(val), obj.sz) || isscalar(val)
obj.Adata=val(:);
else
obj.Adata=val;
end
end
function A=get.A(obj)
A=obj.Adata;
end
function bar(obj)
parfor i=1:1
obj.A(1),
end
end
end
end
3 个评论
Matt J
2016-4-17
编辑:Matt J
2016-4-17
I've edited my post with a variation that does the reshaping in the set.A method.
It might be a bug, and certainly wouldn't hurt to report. My theory, though, is that the warning you were getting about set.A is somehow the cause of the parfor error. When A is converted to a Dependent property with proper set/get methods defined, I see no error thrown by bar().
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!