如何在Matlab中查找一个句柄图形对象的所有属性、方法、事件?
How to find all properties, methods, events of a handle graphics object in Matlab?
我最近在 Matlab 中使用 DataTipEvent
为绘图添加细节。我想根据数据索引向数据提示添加描述性文本。我在 datacursormode
、UpdateFcn
中放置了一个断点,然后尝试检查 event_obj
。这是一个例子。
x = 1:10;
y = 1:10;
fh = figure;
ax = axes(fh);
ha = scatter(ax, x, y);
dcm = datacursormode(fh);
dcm.UpdateFcn = {@myupdatefcn};
function output_txt = myupdatefcn(~, event_obj)
xPos = event_obj.Position(1);
yPos = event_obj.Position(2);
dName = event_obj.Target.DisplayName;
output_txt = {dName, ['X: ',num2str(xPos,4)],...
['Y: ',num2str(yPos,4)]};
end
我假设通过在 myupdatefcn
中放置一个断点我可以检查 event_obj
。这是我发现的。
K>> properties(event_obj)
Properties for class matlab.graphics.internal.DataTipEvent:
Target
Position
K>> methods(event_obj)
Methods for class matlab.graphics.internal.DataTipEvent:
DataTipEvent get getdisp set setdisp
Methods of matlab.graphics.internal.DataTipEvent inherited from handle.
K>> methods('handle')
Methods for class handle:
addlistener eq findprop gt le ne
delete findobj ge isvalid lt notify
K>> superclasses(event_obj)
Superclasses for class matlab.graphics.internal.DataTipEvent:
matlab.mixin.SetGet
handle
这样做的动机是找出 Matlab 使用什么作为返回数据的索引。如您所见,输出不包含与此相关的任何内容。但是 event_obj.DataIndex
给出了该索引。我通过互联网搜索发现 DataIndex
,询问有关从事件对象获取数据索引的问题。我认为必须有一种方法可以直接轮询对象。
显示对象所有信息的正确方法是什么?
我不知道是否有 "proper" 方法来获取有关对象的更多信息,因为 () 一些细节不应该为用户所知。但是,有一些 "improper" 方式(即未记录 and/or 不建议)可以在不同版本之间任意更改:
使用uiinspect
utility from Yair Altman, described in more detail on his blog, Undocumented Matlab。这将向您显示所有方法、属性等。
将对象传递给回调函数中的 struct
to convert it to a structure. This will show you all the properties as fields of a structure, as well as yell at you with a bright orange warning (which you can turn off with warning('off', 'MATLAB:structOnObject')
). For example, we can create an object of the event data class (found using class
)然后将其转换为结构以查看其他属性:
>> obj = matlab.graphics.internal.DataTipEvent % The event data object
obj =
DataTipEvent with properties:
Target: []
Position: []
>> struct(obj)
Warning: Calling STRUCT on an object prevents the object from hiding its
implementation details and should thus be avoided. Use DISP or DISPLAY
to see the visible public details of an object. See 'help struct' for
more information.
ans =
struct with fields:
Target: []
Position: []
DataIndex: []
InterpolationFactor: []
DataTipHandle: []
还有您已经找到的 DataIndex
属性 以及其他两个隐藏属性。
我最近在 Matlab 中使用 DataTipEvent
为绘图添加细节。我想根据数据索引向数据提示添加描述性文本。我在 datacursormode
、UpdateFcn
中放置了一个断点,然后尝试检查 event_obj
。这是一个例子。
x = 1:10;
y = 1:10;
fh = figure;
ax = axes(fh);
ha = scatter(ax, x, y);
dcm = datacursormode(fh);
dcm.UpdateFcn = {@myupdatefcn};
function output_txt = myupdatefcn(~, event_obj)
xPos = event_obj.Position(1);
yPos = event_obj.Position(2);
dName = event_obj.Target.DisplayName;
output_txt = {dName, ['X: ',num2str(xPos,4)],...
['Y: ',num2str(yPos,4)]};
end
我假设通过在 myupdatefcn
中放置一个断点我可以检查 event_obj
。这是我发现的。
K>> properties(event_obj)
Properties for class matlab.graphics.internal.DataTipEvent:
Target
Position
K>> methods(event_obj)
Methods for class matlab.graphics.internal.DataTipEvent:
DataTipEvent get getdisp set setdisp
Methods of matlab.graphics.internal.DataTipEvent inherited from handle.
K>> methods('handle')
Methods for class handle:
addlistener eq findprop gt le ne
delete findobj ge isvalid lt notify
K>> superclasses(event_obj)
Superclasses for class matlab.graphics.internal.DataTipEvent:
matlab.mixin.SetGet
handle
这样做的动机是找出 Matlab 使用什么作为返回数据的索引。如您所见,输出不包含与此相关的任何内容。但是 event_obj.DataIndex
给出了该索引。我通过互联网搜索发现 DataIndex
,询问有关从事件对象获取数据索引的问题。我认为必须有一种方法可以直接轮询对象。
显示对象所有信息的正确方法是什么?
我不知道是否有 "proper" 方法来获取有关对象的更多信息,因为 (
使用
uiinspect
utility from Yair Altman, described in more detail on his blog, Undocumented Matlab。这将向您显示所有方法、属性等。将对象传递给回调函数中的
struct
to convert it to a structure. This will show you all the properties as fields of a structure, as well as yell at you with a bright orange warning (which you can turn off withwarning('off', 'MATLAB:structOnObject')
). For example, we can create an object of the event data class (found usingclass
)然后将其转换为结构以查看其他属性:>> obj = matlab.graphics.internal.DataTipEvent % The event data object obj = DataTipEvent with properties: Target: [] Position: [] >> struct(obj) Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information. ans = struct with fields: Target: [] Position: [] DataIndex: [] InterpolationFactor: [] DataTipHandle: []
还有您已经找到的
DataIndex
属性 以及其他两个隐藏属性。