[xo,yo] = meshgrid(1:4,1:5);
colors = rand(numel(xo),3)
labels = compose('[%.2f %.2f %.2f]',colors)
rectangle(ax, 'Position', [xo(i), yo(i), .6, .6], ...
'FaceColor', colors(i,:), ...
'UserData', labels(i), ...
'ButtonDownFcn', @rectButtonDownFcn);
function rectButtonDownFcn(rectObj, hit)
hitPoint = hit.IntersectionPoint;
ax = ancestor(rectObj,'axes');
holdStates = ["off","on"];
cleanup = onCleanup(@()hold(holdStates(holdstate+1)));
preexisting = findobj(ax,'Tag','TempDataTipMarker');
nAxes = numel(axis(ax))/2;
hh=plot(ax,hitPoint(1),hitPoint(2),'k.','Tag','TempDataTipMarker');
dt = datatip(hh, hitPoint(1), hitPoint(2),'Tag','TempDataTipMarker');
hh=plot(ax,hitPoint(1),hitPoint(2),hitPoint(3),'k.','Tag','TempDataTipMarker');
dt = datatip(hh, hitPoint(1), hitPoint(2), hitPoint(3),'Tag','TempDataTipMarker');
dt.DeleteFcn = @(~,~)delete(hh);
dtr = dataTipTextRow('Color:', repelem(rectObj.UserData,1,4));
hh.DataTipTemplate.DataTipRows(end+1) = dtr;
[xo,yo] = meshgrid(1:4,1:5);
colors = rand(numel(xo),3)
labels = compose('[%.2f %.2f %.2f]',colors)
rectHandles = gobjects(size(xo));
rectHandles(i) = rectangle(ax, ...
'Position', [xo(i), yo(i), .6, .6], ...
'FaceColor', colors(i,:), ...
'UserData', labels(i), ...
'DeleteFc', @(h,~)set(ancestor(h,'figure'), 'WindowButtonMotionFcn',''));
hFig.WindowButtonMotionFcn = {@rectButtonMoFcn, rectHandles(:), ax};
function rectButtonMoFcn(~, ~, rectObj, axHandle)
persistent floatingMarkerHandle
if isempty(floatingMarkerHandle) || ~isvalid(floatingMarkerHandle)
floatingMarkerHandle = plot(axHandle,rectObj(1).Position(1),rectObj(1).Position(2),'k.','Visible','off');
datatip(floatingMarkerHandle, floatingMarkerHandle.XData, floatingMarkerHandle.YData);
dtr = dataTipTextRow('Color:', repelem({'[0 0 0]'},1,4));
floatingMarkerHandle.DataTipTemplate.DataTipRows(end+1) = dtr;
rectPos = cell2mat(get(rectObj,'position'));
rectX = [rectPos(:,1), rectPos(:,1)+rectPos(:,3)];
rectY = [rectPos(:,2), rectPos(:,2)+rectPos(:,4)];
cp = axHandle.CurrentPoint(1,1:2);
rectIdx = cp(1) >= rectX(:,1) & ...
cp(2) >= rectY(:,1) & ...
set(floatingMarkerHandle,'XData',cp(1),'YData',cp(2),'Visible','on');
rectUserData = rectObj(rectIdx).UserData;
floatingMarkerHandle.DataTipTemplate.DataTipRows(end).Value = rectUserData;
set(floatingMarkerHandle,'Visible','off');