draw points using mouse
Mostra commenti meno recenti
Hi,
I want draw points using mouse and then make matlab plot using a specified marker please
Thanks to you
2 Commenti
Geoff Hayes
il 5 Mag 2014
Seldeeno - please provide some more details and/or steps on what you are trying to accomplish. Are you clicking on something (an image perhaps) and then want MATLAB to overlay a marker of some kind?
Seldeeno
il 5 Mag 2014
Risposte (1)
Geoff Hayes
il 5 Mag 2014
You can try this: create a script called myplotter.m. Within this script, add the following code:
% close any open figures
close all;
% launch a new figure and click the mouse as many time as you want within figure
disp('Click the mouse wherever in the figure; press ENTER when finished.');
mousePointCoords = ginput;
% plot the mouse point coordinates on the figure
plot(mousePointCoords(:,1), mousePointCoords(:,2),'b.','MarkerSize',8);
For details on ginput, type help ginput. The above should be sufficient in getting you started on your problem.
5 Commenti
Image Analyst
il 6 Mag 2014
Unfortunately ginput() doesn't drop down a marker as you click so you don't know where you've clicked. And impoint() is not really suited to this, even though it does drop down a marker. Any other ideas?
Geoff Hayes
il 6 Mag 2014
Modificato: Geoff Hayes
il 6 Mag 2014
True, but it will plot a set of points collected from mouse-clicks.
As for an alternative (and I'm not suggesting that this is a good one!)…if your MATLAB is running on Windows, you can do something like what is discussed in http://www.mathworks.com/help/matlab/matlab_external/using-events.html#brhdkzm which discusses the creation of an ActiveX control. What is interesting about this is that the control can respond to mouse events.
Suppose you have a script names mouseclicker.m with the following code:
global figToPlotPtsHandle;
figToPlotPtsHandle = figure;
axis([0 200 0 200]);
f = figure('position', [100 200 200 200]);
obj = actxcontrol('mwsamp.mwsampctrl.2', [0 0 200 200], f, ...
{'MouseDown' 'mymoused'});
In the above, there is a figure to plot the points to, figToPlotPtsHandle, with its axes set as indicated. The second figure is the ActiveX control, of size [0 0 200 200] , that responds to the MouseDown event. This control will be a small figure with a white background and a circle in the centre (just because this is what the code for this control defines). Running this script then will create two figures - the one with ActiveX control for to capture mouse-clicks and a second figure to plot those clicks.
The second set of code to be written is that for the MouseDown event. Create a file called mymoused with the following code:
function mymoused(varargin)
global figToPlotPtsHandle;
% get the x and y coordinates of the mouse down event
x = varargin{5};
y = 200-varargin{6}; % subtract from 200 to account for different
% coordinate systems in the ActiveX control
% and the MATLAB plot
% set the current figure to that which we wish to plot on
set(0,'currentfigure', figToPlotPtsHandle);
% plot the point on the figure
hold on;
plot(x,y,'b.','MarkerSize',8);
end
So if you run the mouseclicker script, two figures appear - the one with the ActiveX control to capture mouse-clicks, and one MATLAB figure to be plotted with points corresponding to the clicks in the other figure/control. It is in the white area of the former figure/control that the user clicks; and it is in the latter figure that those clicks appear as blue dots.
I suspect the ActiveX control can be modified (the C/C++ code seems to be there with instructions to re-compile) so the basic idea above can be expanded for something more complicated. (It can probably be updated to paint clicked points on it as well so the two figures are in synch with respect to plotted points.)
Jon
il 15 Gen 2021
I realize this is a very old thread, but regarding Image Analyst's comment regarding not marking points as they are clicked, you could modify Geoff's code as follows. Mmaybe there are better alternatives since this thread is quite old, if so I would appreciate any ideas as I am currently working on a project that needs this capability.
% define maximum number of allowable mouse clicks to avoid infinite loop
% and also to preallocate array to hold click data
maxPoints = 20;
% define plot limits so it doesn't keep rescaling which might be confusing
xLimits = [0 100];
yLimits = [0 100];
% open new blank figure with defined limits
figure
xlim(xLimits)
ylim(yLimits)
hold on
%
% instruct user on how to enter points and how to terminate
disp('Click the mouse wherever in the figure; press ENTER when finished.');
% preallocate array to hold mouse click coordinates
mousePointCoords = zeros(maxPoints,2);
% set up loop to collect and display mouse click points
count = 0;
for k = 1:maxPoints
% get the mouse click point, or terminate if user presses enter
% in which case the coordinates will be returned empty
coords = ginput(1);
if isempty(coords)
break
end
count = count + 1;
mousePointCoords(count,:) = coords;
plot(coords(:,1),coords(:,2),'b.','MarkerSize',8);
end
% clean up
hold off
mousePointCoords = mousePointCoords(1:count,:); % trim off unused array
% display results
disp(mousePointCoords)
Chance Emanuels
il 30 Dic 2021
@Jon How would I use the mousePointCoords to calculate the area within the points?
Jon
il 4 Gen 2022
Hi Chance,
It has been awhile since I've worked on this. I'd have to review it myself to see what it is all about. Let me know if you are still looking for an answer on this. If it is really a new question, rather than just an extenstion of what's been posted, maybe it would be good to post it as a new question, to get other's input too.
Categorie
Scopri di più su Line Plots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!