Why do we need to use dynamic properties?

조회 수: 4 (최근 30일)
fa wu
fa wu 2023년 9월 5일
댓글: fa wu 2023년 9월 7일
Why do we need to use dynamic properties? In what situations must we use dynamic properties? What value do they bring? If I must add a property, why not add it within the class instead of adding a dynamic property to the object?
The book I'm reading provides examples of dynamic properties, but I find that they seem to function like regular variables. Furthermore, it seems like this program can achieve its goals without dynamic properties. Please take a look at the code below.
classdef MyButton < dynamicprops
properties
UiHandle
end
methods
function obj = MyButton(pos)
if nargin > 0
if length(pos) == 4
obj.UiHandle = uicontrol('Position',pos,...
'Style','pushbutton');
else
error('Improper position')
end
end
end
end
end
The book instructs to execute the following commands.
hbutton1 = MyButton ([20 40 100 40]);
addprop(hbutton1,'Caption');
hbutton1.Caption='OK';
set(hbutton1.UiHandle,'String',hbutton1.Caption)
However, I found that removing dynamic properties does not affect the outcome.
hbutton1 = MyButton ([20 40 100 40]);
hbutton1.UiHandle.String='OK'
  댓글 수: 9
fa wu
fa wu 2023년 9월 6일
@Stephen23 I feel that you must have appreciated the advantages of dynamic properties. I'm looking forward to you providing a formal answer with example code.
fa wu
fa wu 2023년 9월 7일
@Stephen23 Does this example fit the situation you mentioned? -------“I often create GUIs where objects (uicontrols, axes, etc) get switched between various states (particularly visibility, view, string, etc) depending on other menu/data selections).”

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

답변 (1개)

Matt J
Matt J 2023년 9월 6일
They are a convenience, not a requirement. From the documentation:
Use dynamic properties to attach temporary data to objects or to assign data that you want to associate with an instance of a class, but not all objects of that class.
  댓글 수: 4
fa wu
fa wu 2023년 9월 6일
Why doubt it--------I don't doubt, I don't undstand.
"Use dynamic properties to attach temporary data to objects or to assign data that you want to associate with an instance of a class, but not all objects of that class."--------It tell me How,don't tell me Why.
Matt J
Matt J 2023년 9월 6일
Well, like I said, it's just so that you can attach data to an object without cluttering the classdef with additional properties that you're only going to use for one application. I don't think there's any more to it than that.

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by