Developing UI Component Classes | post-setup initialisation method ?

조회 수: 6 (최근 30일)
Amos
Amos . 2021년 2월 1일
편집: Amos . 2023년 6월 6일
Regarding classes derived from matlab.ui.componentcontainer.ComponentContainer:
¿Does there exist (or is there a way of defining) a method which executes:
  • once only;
  • after the setup method;
  • before the update method?
Motivation is (irreversible) object initialisation conditional on one or more property values passed to the constructor.
To this end, the setup method is unsuitable since
"Any property values passed as name-value pair arguments to the UI component's constructor method are assigned after the setup method executes."
The update method is by definition, not a one-shot method.

채택된 답변

Amos
Amos 2022년 9월 6일
편집: Amos 님. 2022년 9월 6일
I'll clarify (and answer) my question with the benefit of hindsight:
I was hoping for a standard method to inject code into the component-constructor, post- (and maybe another, pre-) invocation of the superclass constructor**, analogous to postSetupFcn for setup.
{** noting the superclass constructor invokes the (overridden) setup method}
Seems this doesn't (yet) exist.
While often such a requirement might be avoided by alternative (better?) design, a direct solution is to explicitly define the component constructor.
  댓글 수: 2
Amos
Amos 2023년 6월 6일
편집: Amos 님. 2023년 6월 6일
Thanks again, Greg
I'm not surprised. To date, however, explicit definition of the constructor has proved the path of least grief - at least for my own (mis)use-cases.
My constructor boilerplate necessarily includes (as did my earlier use of postSetupFcn) a guard-clause testing for a figure-ancestor.
Fwiw, finding R2023a more robust than R2022a,b. Nevertheless, appdesigner seems quite cable of crashing itself and MATLAB when loading a problematic mlapp file, particularly as a sub-component (even if lintless and running in isolation). Not the most reassuring feature when developing complex apps with mulitple layers of custom components.

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

추가 답변 (1개)

Greg
Greg 2021년 4월 15일
Your part about "before the update method" is a moving target. The update method is supposed to execute during a graphics flush (i.e., drawnow or pause calls). So depending on the code, your update method might occur more often than another.
In my componentcontainers, I've found the update method completely useless (and even detrimental). I leave it blank, and build my own method named draw that I can call if and when I want it to execute. A trick I use to accomplish the "do stuff before update" is a "skipUpdate" property. Define it as true by default, then the bottom of the constructor sets it to false. Inside the update method, the very first thing is if obj.skipUpdate;return;end.
  댓글 수: 1
Amos
Amos 2022년 9월 6일
편집: Amos 님. 2022년 9월 6일
Belated thanks, Greg.
Apologies - in retrospect, my reference to the update method was a red herring.
I must say, I've found myself also avoiding update wherever possible.
Cheers

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

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by