Search code examples
matlabgraphicsfunction-handle

how to get tag of a clicked rectangle in matlab


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?


Solution

  • 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