0
votes

I am new to matlab. I already have a matrix open in imagesc function. And I want to plot lines by the points gathered from mouse clicks.

This is what I have currently

load('maze', 'A');
% adjust coordinate
hold on; 
plot(1,1);
imagesc(A);
% get points from mouse
[x,y] = ginput;
% graph the line
line(x, y, 'Color', 'r');

Now, what I want to achieve is to plot in real time not after finishing all the clicking. If you click once, you see a point on the map, and click twice you see a line on the map. How should I be able to achieve this?

1

1 Answers

1
votes

if you write ginput(1), then, only one point would be recorded. After your click, the line would be plotted. To plot polyline you would need to plot new line after each click. You can use loop to do the job:

A = zeros(100);
% adjust coordinate
hold on;
plot(1,1);
imagesc(A);
% get points from mouse
x = [];
y = [];
while true
    [x1,y1] = ginput(1);
    x = [x;x1];
    y = [y;y1];
    delete(h);
    % graph the line
    h = line(x, y, 'Color', 'r');
end
hold off

I used infinite loop; you might want to use some other loop best suited for your purposes.

line(x, y, 'Color', 'r'); would plot an additional polyline after each iteration. After 10 clicks, you would have 10 polylines on your plot. To avoid this we give our line name h and delete it before putting a new polyline on the figure. HTH