Filter löschen
Filter löschen

Problems with parfor in class method

3 Ansichten (letzte 30 Tage)
Bananach
Bananach am 17 Apr. 2016
Bearbeitet: Matt J am 17 Apr. 2016
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".

Akzeptierte Antwort

Matt J
Matt J am 17 Apr. 2016
Bearbeitet: Matt J am 17 Apr. 2016
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 Kommentare
Bananach
Bananach am 17 Apr. 2016
Also, note my edit: I can fix my code by declaring "sz" first
Matt J
Matt J am 17 Apr. 2016
Bearbeitet: Matt J am 17 Apr. 2016
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().

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by