Reputation: 569
This is briefly my question: If I have an existing 2D simple matlab figure, I would like to create new points by clicking (or by double-clicking) to a certain point. In addition, I would like to execute a script, or function, or even a command every time I create a new point. I did search both on the web and here carefully but I had no luck...
Could you help please? Thanks in advance!
Upvotes: 1
Views: 1209
Reputation: 1832
something I found-> you need the ButtonDown-Callback:
ax=axes;%you need the handle of the axes
%to give additional values to the callback-fcn use:
set(ax,'ButtonDownFcn',{@mytestcallback,mydata})
%if you dont want to give extra values:
set(ax,'ButtonDownFcn',@mytestcallback)
next you have to define the function mytestcallback like that:
function mytestcallback(h,eventdata)
point = get(h,'currentpoint');
now, within this function you can do whatever you want, like executing another script...
EDIT
what I tried:
function test_mouse
h=figure;
plot(1:10)
myax=findall(h,'Type','axes')
set(myax,'ButtonDownFcn',@mytestcallback)
end
function mytestcallback(h,eventdata)
point = get(h,'currentpoint')
end
what I get, when clicking on the axes:
suprisingly, there is a matrix, like:
point =
3.0219 1.2500 1.0000
3.0219 1.2500 -1.0000
which contains the coordinates of x,y,z with a z=[1,-1]; I guess you can ignore that, if you just have 2d-data
Now, if you want to add new values, you have to get the line in the plot, get the data and update it. I would do something like:
1. get the handle of the line myline=findall(myax,'Type','line') % be careful, this will return all lines (if you have more then one)
2. get the data of the line:
xdat=get(myline,'XData')
ydat=get(myline,'YData')
3. manipulate the data: -> well this depends on what you want to do (insert, append...)
xdat=newdata...
4. update data:
set(myline,'XData',xdat)...
Upvotes: 2