Problems with parfor in class method

6 ビュー (過去 30 日間)
Bananach
Bananach 2016 年 4 月 17 日
編集済み: Matt J 2016 年 4 月 17 日
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".

採用された回答

Matt J
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 件のコメント
Bananach
Bananach 2016 年 4 月 17 日
Also, note my edit: I can fix my code by declaring "sz" first
Matt J
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 ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by