Stuart’s MATLAB Videos

Adding callbacks to lines and axes in MATLAB 6

Posted by Doug Hull,

A callback is a function that gets called when you perform certain actions. For instance, you might want something to happen when you click on a line in MATLAB, or when you move the mouse in MATLAB. These events will trigger a function if you define the buttondownfcn of the line or the windowbuttonmotionfcn of the figure. This short video shows you how to use a function handle to define these callbacks.

6 CommentsOldest to Newest

What if there are multiple lines and we want to change the color of only the one we clicked on. Setting buttonDownFcn of each line separately does not sound practical.

If I had multiple lines e.g. h=plot(rand(5,5);

I can set their callback with one command: set(h,’ButtonDownFcn’,@changeColor);

Only the line you click on will change color. Is this what you mean?

Is it possible to change the color of a line when the mouse cursor hovers on it, without clicking. I could not see a “mouse over” kind of functionality in the documentation.

Add a breakpoint in your callback routine and take a look at the properties of event_obj. You should be able to get the color of the line either from it or its parent.

Add A Comment

What is 3 + 8?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.