Matlab: Plot points and make them clickable to display informations about it

前端 未结 2 1429
离开以前
离开以前 2020-12-30 16:40

I have some points like this:

matrix = rand(6, 4)
0.8147    0.2785    0.9572    0.7922
0.9058    0.5469    0.4854    0.9595
0.1270    0.9575    0.8003    0.6         


        
相关标签:
2条回答
  • 2020-12-30 17:06

    MATLAB figures have a feature called data cursors. On the toolbar, there's a button that looks like a a blue curve, with a crosshair above it and a little tooltip. If you click this and then select one of the points you plotted, you'll get a little tooltip above the point giving some information about that point. You can double-click on the tooltip to pick it up, and rag it across to other plotted points.

    By default, the tooltip displays simple information about the points, namely their X and Y coordinates. But you can customise the text displayed to whatever you want, by getting a handle to the datacursormode object of the figure used for plotting, and setting its UpdateFcn. The UpdateFcn callback is executed to determine the text displayed on the tooltip - in your case it could get the corresponding values from the third and fourth columns of your matrix, splice them together with the string "information", and return that for display.

    See this example in the documentation to see how that can be done in more detail.

    0 讨论(0)
  • 2020-12-30 17:20

    While Sam's method is probably the correct solution here, I'd like to offer another one (although it is more of a 'hack' than a proper solution).

    You can attach context menus to handle graphics objects. These menus can display multiple selections and even let your script respond to user selections. Take a look at the following example:

    x = [1:10];
    y = x.^2;
    
    plot(x,y); hold on;
    h = plot(x(5), y(5),'ro'); %% save the handle to the point we want to annotate
    
    hcmenu = uicontextmenu;
    item1 = uimenu(hcmenu, 'Label', 'info 1');
    item2 = uimenu(hcmenu, 'Label', 'info 2');
    item3 = uimenu(hcmenu, 'Label', 'info 2');
    
    set(h, 'uicontextmenu', hcmenu);
    

    When you right click on the 'o' point, you get the context menu:

    Produces this...

    More information can be found at the Mathwork's site.

    0 讨论(0)
提交回复
热议问题