Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Matlab图形中添加工具提示或覆盖文本_Matlab_Tooltip_Overlay_Matlab Figure_Mouseover - Fatal编程技术网

如何在Matlab图形中添加工具提示或覆盖文本

如何在Matlab图形中添加工具提示或覆盖文本,matlab,tooltip,overlay,matlab-figure,mouseover,Matlab,Tooltip,Overlay,Matlab Figure,Mouseover,我有一个有两行或更多行的图形。这些线具有与之相关的其他重要信息,如创建线所需的平均数据点数量等。我想在我的图中访问这些信息 我认为一个很好的解决方案是,如果你能用鼠标悬停在一条线上,并获得扩展信息 然而,在图形上搜索工具提示/覆盖/悬停似乎没有什么效果 例如: figure; hold on; plot(1:10,rand(10,1)) plot(1:10,rand(10,1)) % additional info plot_1_info.name = 'Alice'; plot_2_info.

我有一个有两行或更多行的图形。这些线具有与之相关的其他重要信息,如创建线所需的平均数据点数量等。我想在我的图中访问这些信息

我认为一个很好的解决方案是,如果你能用鼠标悬停在一条线上,并获得扩展信息

然而,在图形上搜索工具提示/覆盖/悬停似乎没有什么效果

例如:

figure; hold on;
plot(1:10,rand(10,1))
plot(1:10,rand(10,1))
% additional info
plot_1_info.name = 'Alice';
plot_2_info.name = 'Bob';
plot_1_info.age = 24;
plot_2_info.age = 12;

有什么好的解决方案或更好的方法吗

您可以更改数据游标行为,此选项具有良好的向后兼容性(我在R2017b中测试了以下内容,在15b中使用了类似的内容)

详情请参阅我的评论:

% Create some data
x = (1:2:20).';
y = rand(10,1);
name = { 'Alice'; 'Alice'; 'Alice'; 'Alice'; 'Bob'; 'Bob'; 'Bob'; 'Chris'; 'Chris'; 'Chris' };
age = [ 24; 24; 24; 24; 12; 12; 12; 17; 17; 17 ];
% Put it in a table, so we have it all together for indexing as plot data
tbl = table( x, y, name, age );

% Create the plot, assign the UserData property to the plot object
f = figure; 
plt = plot( x, y );
plt.UserData = tbl;

% Hijack the Data Cursor update callback so we can inject our own info
dcm = datacursormode( f );
set( dcm, 'UpdateFcn', @onDataCursor );

% Function which returns the text to be displayed on the data cursor
function txt = onDataCursor( ~, evt )
    % Get containing figure
    f = ancestor( evt.Target, 'figure' );
    % Get the index within the original data
    idx = getfield( getCursorInfo( datacursormode( f ) ), 'DataIndex' );
    % The original data is stored in the UserData property
    data = evt.Target.UserData;
    % Each element of the cell array is a new line on the cursor
    txt = { sprintf( 'X: %g', data.x(idx) ), ...
            sprintf( 'Y: %g', data.y(idx) ), ...
            sprintf( 'Name: %s', data.name{idx} ), ...
            sprintf( 'Age: %g', data.age(idx) ) };          
end
输出:

注意:我没有处理过有多个数据游标提示的情况。您可以轻松地在回调中实现一个循环
idx
来处理这个问题,我把它作为一个练习


这种方法非常灵活。例如,如果我们有3行(每个“person”一行),那么它们可以各自有自己的
UserData
struct,并且我们不需要重复表行中的所有信息

A = struct( 'X', 1:4, 'Y', rand(1,4), 'Name', 'Alice', 'Age', 24 );
B = struct( 'X', 1:3, 'Y', rand(1,3), 'Name', 'Bob', 'Age', 12 );
C = struct( 'X', 1:3, 'Y', rand(1,3), 'Name', 'Chris', 'Age', 17 );

f = figure; hold on;
plt = plot( A.X, A.Y ); plt.UserData = A;
plt = plot( B.X, B.Y ); plt.UserData = B;
plt = plot( C.X, C.Y ); plt.UserData = C;

% ... Now the struct fields can be accessed from the callback
使用,我们可以执行以下操作:

figure();hP=绘图(1:10,兰德(10,1),1:10,兰德(10,1));
nPts=cellfun(@numel,{hP.XData});
hP(1).DataTipTemplate.DataTipRows(end+1)=dataTipTextRow('Name',repmat('Alice',nPts(1),1));
hP(1).DataTipTemplate.DataTipProws(end+1)=DataTipExtrow('Age',repmat(12,NPT(1),1));
hP(2).DataTipTemplate.DataTipRows(end+1)=dataTipTextRow('Name',repmat('Bob',nPts(2,1));
hP(2).DataTipTemplate.DataTipProws(end+1)=DataTipExtrow('Age',repmat(24,NPT(2),1));
%(当然,使用函数可以更好地组织上述内容)
这将产生:


请注意,“悬停数据提示”有黑色文本,“单击数据提示”有蓝色文本-这是默认行为。

2个问题:1)您使用的是哪个MATLAB版本?2) UIFigures是你的一个选择吗?1)R2018b更新3,最新稳定的,如果我是正确的2)我不知道他们,我想,但我更喜欢尽可能简单的方法1)最新稳定的是R2019a;)2) 就这么简单。。。更多关于与现有代码/图的兼容性的问题(uifigures是“新”图)。两个答案都很棒,选择Wolfie的一个作为后向兼容性,两个都应该是正确的。值得一提的是。@Dev iL可以随意发布一个答案,我只有R2017b用于测试,OP没有R2019a。谢谢你的回答,我会试试的@Dev iL:我现在正在升级,请发布你的answe:)我想我理解这个脚本,但它相当复杂。我假设,用我的例子,我可以选择
hP=findall(gcf,'type','axes')
,事情也会是一样的?几乎-
hP=findall(gcf,'type','Line')
。你能添加/编辑将
char
转换成
string
,这对Alice和Bob来说是最简单的解决方案吗