Audel
Audel

Reputation: 2509

How to select an axes child in MATLAB?

I have been looking everywhere but I can't find a site that shows how to do this. The thing I want is to be able to select an object from an axes when I click it, so that I can change its colours and stuff.

I just can't figure out how to select a child, I can create objects but not select them.

I have this piece of code I use to create a line:

coord = ginput (2)
x = coord(:,1)
y = coord(:,2)
hline = line(x,y)

I'm not sure If i need to create the objects in an array so that I can select edit/delete them. I believe I would need to use ButtonDownFcn, but probably I'm doing something completely wrong.

Any help would be appreciated, If I'm missing any information please let me know

Thanks

Upvotes: 1

Views: 2868

Answers (1)

O. Th. B.
O. Th. B.

Reputation: 1353

It is not necessary to use ginput and extract the coordinates. This is done automatically by an built-in "listener" in the figure-window. You are correct in assuming that you can use the ButtonDownFcn property on the object (line, lineseries, or other handle graphics object).

Try to create at simple line from (0,0) to (1,1):

hline = line([0,1],[0,1]) %# create line, save handle in hline

Then you can set the ButtonDownFcn to, for instance, a function handle to an anonymous function:

set( ...
   hline, ...
   'ButtonDownFcn', @(handle,event)(disp(['You clicked on the line!'])) ...
);

Now try to click on the line. It should print the text in the command window.

The function needs to be able to receive atleast two arguments: (1) the handle of the object itself (the line) and (2) an "event structure". I believe the second argument is just empty when you use line-objects. But your function still needs to receive atleast these two arguments (even if you do not use them).

Read more here: http://www.mathworks.com/help/techdoc/ref/line_props.html.

You can also use your own function (a named function in a file):

set( ...
   hline, ...
   'ButtonDownFcn', @(handle,event)(namedFunction(handle,event)) ...
);

... or you can use a struct-array if you (expectedly) have other arguments beyound those two.

Upvotes: 2

Related Questions