Matt
Matt

Reputation: 2802

How to find all properties, methods, events of a handle graphics object in Matlab?

I recently was working with a DataTipEvent in Matlab to add details to a plot. I wanted to add descriptive text to a data tip based on the index of the data. I placed a break point in the datacursormode, UpdateFcn then attempted to inspect the event_obj. Here is an example.

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

I assumed that by placing a breakpoint in myupdatefcn I could inspect event_obj. This is what I found.

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

The motivation for doing this was to find out what Matlab used for the index of the data returned. As you can see, the output doesn't contain anything related to that. However event_obj.DataIndex gives that index. I found DataIndex through internet searches asking about getting data index from an event object. I figure there must be a way to poll the object directly.

What is the proper way to show all the information about objects?

Upvotes: 2

Views: 517

Answers (1)

gnovice
gnovice

Reputation: 125874

I don't know if there's a "proper" way to get more information about objects, since (as Cris mentions) some details aren't meant to be known to the user. However, there are some "improper" ways (i.e. undocumented and/or not suggested) which could arbitrarily change from version to version:

  • Use the uiinspect utility from Yair Altman, described in more detail on his blog, Undocumented Matlab. This will show you all the methods, properties, etc..

  • Pass the object to 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 inside your callback function) then convert it to a structure to see the other properties:

    >> 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: []
    

    And there's the DataIndex property you already found, along with two other hidden properties.

Upvotes: 1

Related Questions