1

I have a set of rectangles in a figure. I'm tagging them by a rect_tag index, and I want to get an array (or cell array) which has the tags of clicked rectangles. The rectangles are generated by:

for i_nf=1:nRects
    rect_tag = ['rectangle_num_' num2str(i_nf)];
    rectangle('Position', rectanglesMat(i_nf,:), 'Tag', rect_tag, 'ButtonDownFcn', {@add_rectangle});
end

How do I define the add_rectangle function to accomplish this?

mousomer
  • 2,632
  • 2
  • 24
  • 25
  • 1
    What are you struggling with? The first argument to the callback-function is the rectangle itself. You presumably know how to get the tag, you just have to add it to some list at a somewhat larger scope. – sebastian Nov 19 '13 at 11:52
  • Well, apparently, I fail. Could you supply a simple working example? You'[d earn my eternal gratitude. – mousomer Nov 19 '13 at 11:55

1 Answers1

1

Thanks, @sebastian. It wokred. For future reference, this is what worked:

function add_rectangle(src, event)
    a = get(src,'Tag')
   if evalin('base', 'exist(''tag_list'',''var'')')
      tag_list= evalin('base','tag_list');
   else 
      tag_list= {};
   end
   class(tag_list)
   tag_list{end+1} = {a}; % add the point
   assignin('base','tag_list',tag_list); % save to base
end
mousomer
  • 2,632
  • 2
  • 24
  • 25
  • 1
    There you go :>. Note that you could also do `tag_list{end+1} = a;` or `tag_list(end+1) = {a};`. Your method stores each tag in a 1x1-cell, which is probably not necessary. – sebastian Nov 19 '13 at 14:25