How to call protected functions in parfor?

4 ビュー (過去 30 日間)
Tmfu Vh
Tmfu Vh 2019 年 7 月 26 日
コメント済み: Tmfu Vh 2019 年 7 月 30 日
This function is inside the common methods block of a class.
function GetWavelets(Me)
%Unrelated codes omitted…
parfor c=1:ttpc
%Me is the object itself, OnProgressReport is protectedly defined.
Me.OnProgressReport(c,ttpc);
%…
end
%…
end
The protected function OnProgressReport is unaccessible in the parfor loop. I found two workarounds:
  • Use for loop instead of parfor;
  • Set the Access attribute of OnProgressReport to public.
If I want to preserve the parfor loop for performance reasons, is it unavoidable to expose the OnProgressReport to public? Any other solutions?
Update 20190728: The OnProgressReport function is inherited from the superclass.

回答 (1 件)

Edric Ellis
Edric Ellis 2019 年 7 月 26 日
編集済み: Edric Ellis 2019 年 7 月 29 日
EDIT: Changed my version to inherit protected method from parent class
Hm, I can't reproduce the problem - I tried in R2016b and R2019a. Here are my test classes:
%% Super.m:
classdef Super
properties (Access = private)
Value = 7
end
methods (Access = protected)
function out = protectedMethod(obj, x)
out = obj.Value + x;
end
end
end
%% Test.m:
classdef Test < Super
methods
function out = runInParfor(obj)
parfor ii = 1:4
out(ii) = obj.protectedMethod(ii);
end
end
end
end
which I exercise like this:
>> runInParfor(Test)
ans =
8 9 10 11
So, it appears to work for me. Could you elaborate on what you're doing differently?
  3 件のコメント
Edric Ellis
Edric Ellis 2019 年 7 月 29 日
Ok, I updated my attempt to reproduce the problem, still works fine for me...
Tmfu Vh
Tmfu Vh 2019 年 7 月 30 日
Well I got it. The exception details showed in the commandline window is misleading - it said that the OnProgressReport function is undefined, but the real bug I found later has nothing to do with that function. So tricky! Whatever thank you for your patience.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreate System Objects についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by