以编程方式设置数据提示? [英] Set data tips programmatically?

查看:90
本文介绍了以编程方式设置数据提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to be able to set data tips programmatically from a list of array of x axis values. For example, I create a figure and plot my data.

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

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

I have a set of timestamp values from t1 variable (time) (e.g. [0.450, 0.854, 1.2343....]) where I want to place data tips to mark certain events in my data. Without having to place them every time manual by clicking and saving data trip... How can I pass them as array and do this programmatically through matlab script?

推荐答案

您可以通过编程方式添加matlab数据提示,并在一定程度上对其进行自定义.

You can add matlab datatip programatically and customize them to an extent.

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

The function below shows how to add a few datatip, position them and customize their display:

此演示的代码(将其保存在文件demo_datatip.m中并运行以获得上图):

The code for this demo (save that in a file demo_datatip.m and run it to obtain the above figure) :

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)(甚至是句柄数组即可在组中删除它们).

If you need to delete a data tip, you can simply call delete(datatip_handle) on it's handle (or even an array of handles to delete them in group).

这篇关于以编程方式设置数据提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆