How to group data objects in matlab figures

1.4k views Asked by At

I am working on making a simple interactive plotter in Matlab. What I am doing is simple: I use the command:

[x,y] = ginput(1)

to pin-point the location (x,y). Then, I use this point as a seed point to plot a curve and also I add some arrows (which show the direction) located on the top of the plotted curve.

Now my question is: After I am done with the figure, I would like to be able to select (and for instance delete) a single curve and ITS corresponding ARROWS with just one single click.

The problem is that those arrows and the curve are not part of the same object and that's why Matlab will only delete the curve or the arrows depending on which one was/were selected, to begin with.

Narrowing it down, I know that the curve and arrows do have separate object handlers. Rephrasing my question: Is there anyway that I could either group these two separate handlers or, for instance, make arrows children of the original curve?

2

There are 2 answers

1
bdecaf On BEST ANSWER

Well you can approach this problem in different ways.

First about storing the informations:

  • Most cleanly would be if you would write your own classes with classdef to hold your objects, there is the class handles you can extend for such purposes. Alternatively you could store them in the userdata fields of all graphic objects.

  • about the selection and deletion - lines can be assosiciated with a ButtonDownFcn and DeleteFcn. The first can be used to highlight the related lines, the second to delete the related ones.

1
H.Muster On

You can use the tag property to group the elements in your plot. For instance, use

hold on;    
plot(x, y, 'tag', 'group1');
plot(x2, y2, 'tag', 'group1');

plot(x3, y3, 'tag', 'group2');
plot(x4, y4, 'tag', 'group2');

Afterwards you can select all elements belonging to the first group by

h = findall(0, 'tag', 'group1');

and delete them by

delete(h); 

As requested by the OP in the comments, here is a solution that allows for interactive group-wise deletion of data points:

   plot(x, y, 'tag', 'group1', 'buttondownfcn', @(obj, evt) delete(findall(gca, 'tag', 'group1')))
   plot(x2, y2, 'tag', 'group2', 'buttondownfcn', @(obj, evt) delete(findall(gca, 'tag', 'group2')))

If you click of one data point now, all points belonging to the same group will get deleted.