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

223*_*112 2 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脚本以编程方式进行?

Hok*_*oki 6

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

下面的函数显示了如何添加一些数据提示,放置它们以及自定义它们的显示:

demo_datatip

该演示的代码(将其保存在文件中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)] ...
                };
Run Code Online (Sandbox Code Playgroud)

如果您需要删除数据提示,则只需调用delete(datatip_handle)它的句柄(甚至是一组句柄即可在group中删除它们)。

  • 我想我找到了解决方案。使用`set(hdtip(idt),'Position',pos)`然后使用`updateDataCursors(cursorMode)`而不是`update(hdtip(idt),pos)`对我有用。 (3认同)