Thewaywewalk给出的永久性答案在R2015a中可能不再有效,可能在以后的版本中也不再有效。 所以在这里我分享临时解决方案和永久解决方案的解决方案
临时解决方案(单个图):
以下函数包含更新函数作为嵌套函数。 调用datacursorextra将其应用于当前图形,或datacursorextra(fig)将其应用于某个图形fig 。
function datacursorextra(fig)
% Use current figure as default
if nargin<1
fig = gcf;
end
% Get the figure's datacursormode, and set the update function
h = datacursormode(fig);
set(h,'UpdateFcn',@myupdatefcn)
% The actual update function
function txt = myupdatefcn(~,event)
% Short-hand to write X, Y and if available Z, with 10 digit precision:
lbl = 'XYZ';
txt = arrayfun(@(s,g)sprintf('%s: %.10g',s,g), lbl(1:length(event.Position)), event.Position,'uniformoutput',false);
% If a DataIndex is available, show that also:
info = getCursorInfo(h);
if isfield(info,'DataIndex')
txt{end+1} = sprintf('Index: %d', info.DataIndex);
end
end
end
永久解决方案(默认情况下适用于所有数据):
我还没有找到为数据游标设置默认UpdateFcn的方法,但是可以添加一些每次创建新图形时都会调用的代码。 startup.m下行添加到您的startup.m :
set(0,'defaultFigureCreateFcn',@(s,e)datacursorextra(s))
并确保上面给出的datacursorextra函数在您的Matlab路径中可用。