Reputation: 301
I am creating a GUI in Matlab and I need to move some data points using the mouse, for example:
imshow(someImage, [ ]), hold on;
plot(x, y, '*r')
I want to select a point from x and y vectors by clicking on it and move it using the mouse. How can I implement it?
Upvotes: 0
Views: 487
Reputation: 13933
You can use impoint
from the Image Processing Toolbox since it already offers you the possibility to drag the point around. Therefore, create a figure and plot whatever you want. Then, call impoint(gca)
so you can place a point on the current axes. After the point is drawn you can drag it around with your mouse. You can call impoint
again for the second point and so on...
To get the position of the points, you want to store them in an impoint
-array upon creation and then call getPosition
with each of the points to get the coordinates.
Since you did not provide any code to extend with the desired functionality, I created a simple plot with two buttons as an example. Press "Add point" which allows you to place a new point. After the first click, you can move this point around. When all the points are added, click "Done" to read the final coordinates of the points.
figure; % create new figure
plot([0,1],[0,0],'r'); % plot something nice (your image)
ylim([-1,1]); % set limit of y-axis
h = impoint.empty; % define empty object array of type impoint
btnAdd = uicontrol('String','Add point',...
'Position',[90 60 70 30],...
'Callback', 'h(end+1)=impoint(gca);h(end).Deletable=0;wait(h(end))');
btnDone = uicontrol('String','Done',...
'Position',[165 60 40 30],...
'Callback', 'uiresume(gcbf)');
uiwait(gcf); % wait until 'Done' is pressed
delete(btnAdd); % revove the button
delete(btnDone); % revove the button
% get coordinates of points
pos = zeros(numel(h),2); % preallocate
for k = 1:numel(h)
pos(k,:) = getPosition(h(k)); % read coordinates of points
end
% evaluate points
positivePoints = sum(pos(:,2)>0) % count all points above 0
Upvotes: 1