Matlab GUI axis buttondownfnc Select points

2 views (last 30 days)
I've got a figure with different points that are related in pairs. When clicking in one of the points I would like to change the marker of that point and also the marker of the related point.
Then when clinking in the axis instead of a point return the markers to its original.

Accepted Answer

Patrick Kalita
Patrick Kalita on 15 Jul 2011
Well this sounded like a fun one, so here's what I came up with:
% Set up my data sets. Each pair of x-coordinates goes into one column of a
% 2-by-N array. Each pair of y-coordinates goes into one column of another 2-by-N
% array.
X = [ rand(1,10); rand(1,10) ];
Y = [ rand(1,10); rand(1,10) ];
% Make an axes and plot the lines. When you give PLOT two arrays like this it
% will make a separate line for each column of data.
a = axes;
p = plot(a, X, Y, 'LineStyle', 'none', 'Marker', 'o', 'MarkerSize', 15, 'Color', 'k');
% When you click on the axes, set all of the marker of all the lines.
set(a, 'ButtonDownFcn', @(src, evt) set( p, 'Marker', 'o' ) );
% When you click on a line, set the marker of just the line you clicked on.
set(p, 'ButtonDownFcn', @(src, evt) set( src, 'Marker', 's' ) );
  2 Comments
Eddie
Eddie on 15 Jul 2011
It works really well. The only problem is that in my original data there were two different markers for different information. Using your method, can I plot the begining of one line with one marker and the end with another???
Thanks, for your help.
Walter Roberson
Walter Roberson on 15 Jul 2011
Not easily, no. You would have to plot the line with no markers at all, and then plot each point with the marker appropriate for it.
The "line" and "lineseries" objects are restricted to one marker type per line.

Sign in to comment.

More Answers (1)

Eddie
Eddie on 15 Jul 2011
Thank you all, Finally I manage a solution. First I plot the lines using different and big markers and then plot the lines with little point markers. Here is the code:
ax1=handles.axes1;
axes(ax1);
X=[ real(sol1(13,:)) real(sol2(13,:)) ; real(sol1(13,:))+real(sol1(1,:)) real(sol2(13,:))+real(sol2(1,:))]
Y=[ imag(sol1(13,:)) imag(sol1(13,:)) ; imag(sol1(13,:))+imag(sol1(1,:)) imag(sol1(13,:))+imag(sol2(1,:))]
p1 = plot(X(1,:),Y(1,:),'^k','MarkerSize',5,'MarkerFaceColor','k');
hold on;
p2 = plot(X(2,:),Y(2,:),'ok','MarkerSize',5,'MarkerFaceColor','k');
p = plot(ax1, X, Y, 'LineStyle', 'none', 'Marker', '.', 'Color', 'k');
axis('equal');
set(p, 'ButtonDownFcn', @(src, evt) set( src, 'Marker', '*', 'MarkerSize',10,'Color', 'r') );
set(ax1, 'ButtonDownFcn', @(src, evt) set( p, 'Marker', '.', 'Color', 'k') );
It is not very elegant; but it does the job.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!