필터 지우기
필터 지우기

Problems with parfor in class method

조회 수: 3 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by