Description of the problem
The data cursor does not work for image objects plotted in App Designer (or in any uifigure) in r2020a. AppDesigner features are still being rolled out with every release of Matlab. For example, data tips have been implemented in r2020a. But for image objects, the datacursor still does not work. Here is a demo of the problem using a legacy figure and a uifigure.
imageData = randi(100,10,12);
fig = figure();
ax = axes(fig);
image(ax,imageData);
ax.Colormap = jet(100);
dcm = datacursormode(fig);
dcm.Enable = 'on';
title(ax, 'Legacy figure')
uifig = uifigure();
uiax = uiaxes(uifig, 'Position', [10 10 500 410]);
image(uiax,imageData);
uiax.Colormap = jet(100);
axis(uiax,'tight')
dcm = datacursormode(uifig);
dcm.Enable = 'on';
title(uiax,'uifigure')
As you can see, the datacursor appears as exected in the legacy figure but in the uifigure, a message appears, Unrecognized method. property, or field 'ColorSpace' for class 'matlab.ui.control.UIAxes'
Solution
Until Matlab suports datacursors with images in UIAxes, you can create a custom update function for the datacursor object within the UIAxes/UIFigure. This demo shows how to recreate the datacursor text produced in the legacy figure.
Continuing from the example above,
dcm = datacursormode(uifig);
dcm.Enable = 'on';
dcm.UpdateFcn = @(hObj,event,ax)localDcmFcn(hObj,event,uiax);
function txt = localDcmFcn(~,event,ax)
idx = event.Target.CData(event.Position(2),event.Position(1));
txt = sprintf('[X,Y] [%d %d]\nIndex %d\n[R,G,B] [%.4g %.4g %.4g]', ...
event.Position, idx, ax.Colormap(idx,:));
end
Now the datacursor should appear the same in the legacy figure and the uifigure/app. The only difference may be the number of decimal places shown in the RGB values.
0 Comments
Sign in to comment.