Matlab: specify points and make them available to display information about it

I have moments 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.6557 0.9134 0.9649 0.1419 0.0357 0.6324 0.1576 0.4218 0.8491 0.0975 0.9706 0.9157 0.9340 

the first two columns are the x and y values ​​that are displayed as dots through

plot(matrix(:, 1), matrix(:, 2), '*r'

Now what I want to solve is the following: Whenever I click on a specific point in the graph, I want the information from columns 3 and 4 to be displayed as text to the right of the point in the field, for example. with some text, for example. information 1: VALUE_COL3, information 2: VALUE_COL4 . How to do it? I thought of the ButtonDownFcn attribute and then looked at the click and matched it. But is there an easier way to do this?

Thanks!

+4
source share
2 answers

MATLAB digits have a function called data cursors. There is a button on the toolbar that looks like a blue curve, with a crosshair above it and a small tooltip. If you click on it and then select one of the points that you drew, you will get a small hint above the point, providing some information about that point. You can double-click the tooltip to pick it up, and thin it out onto other plotted points.

By default, a tooltip displays simple information about points, namely their X and Y coordinates. But you can customize the text displayed as you wish by receiving a handle to the datacursormode object of the figure used to plot the graph and set it to UpdateFcn . The UpdateFcn is performed to determine the text displayed in the tooltip - in your case, it can receive the corresponding values ​​from the third and fourth columns of your matrix, combine them with the “information” line and return them for display.

See this example in the documentation for how to do this in more detail.

+6
source

Although Sam's method is probably the right solution here, I would like to suggest one more (although it is more likely to “hack” than the right solution).

You can connect context menus for processing graphic objects. These menus can display several options and even allow your script to respond to user choices. 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 point "o", you get a context menu:

Produces this ...

More information can be found on the Mathwork Website .

+7
source

Source: https://habr.com/ru/post/1385490/


All Articles