Dependent property is shown in the variable editor even before its get method

조회 수: 1 (최근 30일)
I have a class with a dependent property. But even before I invoke its get method, its value is shown in the Variable Editor. How can this happen, isn't a dependent property calculated on demand (i.e. calling its get method)? For an actual code, see e.g. the documentation .

채택된 답변

per isakson
per isakson 2016년 1월 31일
편집: per isakson 2019년 3월 6일
"before I invoke its get method, its value is shown" Yes, that's is obviously the case, but I fail to find this behaviour described in the documentation. However, I think it's useful.
IIRC: The dependent property is calculated "in the background" when displayed in a tooltip or the Variable Editor. Several releases back, the dependent property was calculated by an explicit call to the get-function when required for a tooltip. That caused a lot of pain when there was a bug in the get-function.
Try the following steps
>> cdp = class_dep_prop();
  • set a breakpoint in the foo-method
>> cdp.foo
  • point at this to see a tooltip. Note that a value is displayed for x, but not for y.
  • replace b=(1:3); by b=(1:2); and repeat the steps. Now, a value is displayed for y.
This behaviour is better than the old one, which throw an error in the get-function. (Maybe, it would be even better to show something like y=-error- in the tooltip when there is a bug in the get-function.)
where
classdef class_dep_prop < handle
properties
x = 1;
end
properties ( Dependent = true )
y
end
methods
function val = get.y( this )
a = (1:2);
b = (1:3);
c = a .* b;
val = c * this.x;
end
function val = foo( this ) %#ok<MANU>
val = 17;
end
end
end
  댓글 수: 3
per isakson
per isakson 2016년 2월 3일
Yes, the value of a dependent property is calculated on-the-fly when needed. The results in not saved in the object. Exactly how this is done is not documented.
The behavior when there is bug in the get method seems to vary between releases. With R2013a no property names or values are displayed in the variable editor. The pane is blank.
Zoltán Csáti
Zoltán Csáti 2019년 3월 7일
Thanks for the updated answer. I tried it and this is a good example. If MATLAB had an issue tracker (like in Github), we could send recommendations and feature requests to Mathworks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by