在绘图上向数据点添加额外信息

时间:2015-05-26 20:14:31

标签: matlab plot matlab-figure

以下是解决方案!

enter image description here

这个(x,y)值实际上对应于t值,我想在图上看到。我该怎么办?

显然,matlab能够在数据点框中显示多个信息:

enter image description here 只需将其称为“TimePlot(x,y,t)”,它就会起作用。我相信,这段代码还说明了修改数据提示的几个关键点。

function  TimePlot( varargin )
x=varargin{1};
y=varargin{2};
t=varargin{nargin};
fh=figure;
plot(varargin{1:nargin-1})

function output_txt = myfunction(obj,event_obj)
% Display the position of the data cursor
% obj          Currently not used (empty)
% event_obj    Handle to event object
% output_txt   Data cursor text string (string or cell array of strings).




pos = get(event_obj,'Position');

ind=intersect(Find(x,pos(1),1e-10),Find(y,pos(2),1e-10));
if(length(ind)~=1)
    text='err';
else
    text=num2str(t(ind),4);
end

output_txt = {['X: ',num2str(pos(1),4)],...
    ['Y: ',num2str(pos(2),4)],['T: ',text]};

% If there is a Z-coordinate in the position, display it as well
if (length(pos) > 2)
    output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end

end

dcm=datacursormode(fh);
datacursormode on
set(dcm,'updatefcn',@myfunction)



end

function [ out ] = Find( vector, value ,precision)

if nargin < 3
    precision =   0.0001;
end

out=[];

for i=1:length(vector)
    
    if(abs(vector(i)-value)<precision)
        out=[out i];
    end

end

end

2 个答案:

答案 0 :(得分:1)

在MATLAB Central上,您可以找到有关如何创建自定义数据提示的广泛视频教程:Tutorial: How to make a custom data tip in MATLAB

  

如果在MATLAB中使用标准数据提示,它将注释数据点的X和Y值。此视频将说明如何自定义该数据提示中显示的信息。

documentation about the datacursormode中,您会找到更多示例(以下全部从doc中复制):

  

此示例在当前图形和集合上启用数据光标模式   数据光标模式选项。以下陈述

     
      
  • 创建图表
  •   
  • 将数据光标模式切换到开启
  •   
  • 获取数据光标模式对象,指定数据提示选项,并获取数据提示占用的行的句柄:
  •   
fig = figure;
z = peaks;
plot(z(:,30:35))
dcm_obj = datacursormode(fig);
set(dcm_obj,'DisplayStyle','datatip',...
    'SnapToDataVertex','off','Enable','on')

disp('Click line to display a data tip, then press Return.')
% Wait while the user does this.
pause 

c_info = getCursorInfo(dcm_obj);
% Make selected line wider
set(c_info.Target,'LineWidth',2)

enter image description here

  

此示例显示如何自定义数据光标的文本   显示器。例如,您可以替换数据中显示的文本   提示和数据窗口(x:和y :)与时间:和幅度:通过创建   一个简单的更新功能。

     

将以下功能保存在当前目录或任何可写内容中   运行它们之前在MATLAB路径上的目录。像他们那样   功能,您不能突出显示它们,然后评估选择   让它们发挥作用。

     

将此代码另存为doc_datacursormode.m:

function doc_datacursormode % Plots graph and sets up a custom data tip update function 
fig = figure; 
a = -16; 
t = 0:60; 
plot(t,sin(a*t)) 
dcm_obj = datacursormode(fig); 
set(dcm_obj,'UpdateFcn',@myupdatefcn)
  

将以下代码保存为MATLAB路径上的myupdatefcn.m:

function txt = myupdatefcn(empt,event_obj) % Customizes text of data tips

pos = get(event_obj,'Position'); txt = {['Time: ',num2str(pos(1))],...
          ['Amplitude: ',num2str(pos(2))]};
  

要设置和使用更新功能,请键入:

doc_datacursormode
  

使用此更新功能放置数据提示时,它看起来像   下图中的那个。

enter image description here

答案 1 :(得分:0)

我认为你不能在二维图中看到第三维的价值。你可以尝试做冲浪(x,y,t)或plot3(x,y,t),这样你就可以得到一个三维图并且有了正确的方向,你可以获得所需的图和所有必需的x,y和t值。 / p>

相关问题