matlab - 以编程方式设置数据提示?

标签 matlab plot matlab-figure

我需要能够从 x 轴值数组列表中以编程方式设置数据提示。例如,我创建了一个图形并绘制了我的数据。

figure;plot(t1,[filter(b,a,Gyro(:,2)),filter(b,a,Gyro(:,4))])

我有一组来自 t1 变量(时间)的时间戳值(例如 [0.450, 0.854, 1.2343....]),我想在其中放置数据在我的数据中标记某些事件的提示。不必每次都通过单击并保存数据来手动放置它们...我如何将它们作为数组传递并通过 matlab 脚本以编程方式执行此操作?

最佳答案

您可以通过编程方式添加 matlab 数据提示并在一定程度上自定义它们。

下面的函数展示了如何添加一些数据提示,定位它们并自定义它们的显示:

demo_datatip

这个demo的代码(保存在demo_datatip.m文件中,运行得到上图):

function h = demo_datatip

    %// basic sample curve
    npts = 600 ;
    x = linspace(0,4*pi,npts) ;
    y = sin(x) ;

    %// plot
    h.fig = figure ;
    h.ax = axes ;
    h.plot = plot(x,y) ;

    %// simulate some event times
    time_events = x([25 265 442]) ; %// events type 1 at index 25, 265 and 422

    %// define the target line for the new datatip
    hTarget = handle(h.plot);

    %// Add the datatip array
    h.dtip = add_datatips( time_events , hTarget ) ;


function hdtip = add_datatips( evt_times , hTarget )
    %// retrieve the datacursor manager
    cursorMode = datacursormode(gcf);
    set(cursorMode, 'UpdateFcn',@customDatatipFunction, 'NewDataCursorOnClick',false);

    xdata = get(hTarget,'XData') ;
    ydata = get(hTarget,'YData') ;

    %// add the datatip for each event
    for idt = 1:numel(evt_times)
        hdtip(idt) = cursorMode.createDatatip(hTarget) ;
        set(hdtip(idt), 'MarkerSize',5, 'MarkerFaceColor','none', ...
                  'MarkerEdgeColor','r', 'Marker','o', 'HitTest','off');

        %// move it into the right place
        idx = find( xdata == evt_times(idt) ) ;%// find the index of the corresponding time
        pos = [xdata(idx) , ydata(idx) ,1 ];
        update(hdtip(idt), pos);
    end

function output_txt = customDatatipFunction(~,evt)
    pos = get(evt,'Position');
    idx = get(evt,'DataIndex');
    output_txt = { ...
        '*** !! Event !! ***' , ...
        ['at Time : '  num2str(pos(1),4)] ...
        ['Value: '   , num2str(pos(2),8)] ...
        ['Data index: ',num2str(idx)] ...
                };

如果你需要删除一个数据提示,你可以简单地在它的句柄上调用delete(datatip_handle)(或者甚至是一组句柄来删除它们) .

关于matlab - 以编程方式设置数据提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882186/

相关文章:

r - 如何制作具有多个几何图形的自定义 ggplot2 geom

python - 如何更新 matplotlib 中的 pcolor?

matlab - MATLAB R2019a中contourf函数中 'ShowText'和 'LineStyle'冲突?

performance - 导入大型矩阵 : Import All or by Column? - MATLAB

matlab - 如何在 Matlab 中显示 100x1 矢量数据的经验 pdf?

r - 如何指定折线图的起点和终点

matlab - 设置默认渲染器

matlab - 找出一幅图像相对于另一幅图像的旋转

matlab - 使用函数/字符串访问复杂的 matlab 结构

matlab - 使用 Matlab 自定义绘图