Atom Feed

Tuesday, April 25, 2006

Creating an Interactive Plot in Matlab



The code below creates a plot where the user can draw a series of connected lines. Afterwards, the points chosen for the lines is displayed as an array in the top right corner of the plot.

function cursor_plot_test(points)
%Sets up figure, fig1 is the handle for the figure
fig1 = figure('Name','Cursor Plot Test', 'Pointer', 'crosshair');


%axes creates a plot with axes having the minimum and maximums

%specified in the square brackets

axes('Xlim',[0 10],'Ylim',[0 10]);


%This retrieves the mouse position in the figure whenever it is left

%clicked. The x_pos and y_pos values are scaled to the axes

positions(1,:) = ginput(1);

for p = 2:points
positions(p,:) = ginput(1); %the 1 in ginput means that it will collect

%one mouse click position. If we put in

%2 then it would collect two mouse
%positions.
line(positions((p-1):p,1), positions((p-1):p,2));

end

%This is why we kept the handle for the figure, so after we done

%choosing our points we could set the pointer for that figure back to

%an arrow.

set(fig1, 'Pointer','arrow');
%Displays the positions we chose as a matrix

text(7, 9.5, sprintf(' X Y \n')); %springf takes in a formatted string
%and outputs it as a string

text(7, 8.5, num2str(positions)); %num2str can convert an array into string

The M-file for this project is at: http://neuralsmith.googlepages.com/cursor_plot_test.m

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home