Matlab UpdateFcn 使用数据游标可视化图中线条的名称
Matlab UpdateFcn to visualize names of the lines in the plot using data cursor
我正在尝试创建自己的函数来显示我在绘图中使用数据游标时需要的内容。更准确地说,我在同一个图中有很多图,当光标悬停在这些图上时,我想为每个图显示一个准确的名称。
编辑:示例:假设我在一个图中有 100 个函数,这些函数有一些名称:f1、f2、f3、....、f100。然后,当我观看情节时,我希望能够在数据游标中看到这些函数中每一个函数的名称。例如,如果我将鼠标悬停在最后一个函数上,我想在数据光标中显示字符串 'f100' 而不是点的坐标。
要做到这一点,我已经看到我应该以这种方式使用 'UpdateFcn':
dcm_obj = datacursormode(gcf);
% Set the UpdateFcn to the function myCursor
set(dcm_obj, 'UpdateFcn', @myfunction);
其中 myfunction 是自定义函数,在 output_txt 中应该给出我想要显示的字符串:
function output_txt = myfunction(~,event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text (string or cell array
% of strings)
在event_obj中有Position和Target,其中Position是一个数组,指定了光标的x,y,(和z)坐标,而Target是图形对象的句柄包含数据点。
更多信息位于:http://it.mathworks.com/help/matlab/ref/datacursormode.html
我想到了一个可能的解决方案:当我在包含数据点(目标)的图形对象的句柄中绘制它时,记住带有每个图名称的字符串,但我不知道它是否是可能,如果是,如何做到这一点。
还有其他解决办法吗?
由于您在调用 plot
时知道名称,因此可以将名称存储在绘图对象的 DisplayName
或 UserData
字段中。然后,您可以从 UpdateFcn
回调中访问这些。
以DisplayName
为例:
plot(rand(10, 1), 'DisplayName', 'a');
plot(rand(10, 1), 'DisplayName', 'b');
plot(rand(10, 1), 'DisplayName', 'c');
function updateFcn(~, event_obj)
name = get(event_obj.Target, 'DisplayName');
% Do something with name here
end
我正在尝试创建自己的函数来显示我在绘图中使用数据游标时需要的内容。更准确地说,我在同一个图中有很多图,当光标悬停在这些图上时,我想为每个图显示一个准确的名称。
编辑:示例:假设我在一个图中有 100 个函数,这些函数有一些名称:f1、f2、f3、....、f100。然后,当我观看情节时,我希望能够在数据游标中看到这些函数中每一个函数的名称。例如,如果我将鼠标悬停在最后一个函数上,我想在数据光标中显示字符串 'f100' 而不是点的坐标。
要做到这一点,我已经看到我应该以这种方式使用 'UpdateFcn':
dcm_obj = datacursormode(gcf);
% Set the UpdateFcn to the function myCursor
set(dcm_obj, 'UpdateFcn', @myfunction);
其中 myfunction 是自定义函数,在 output_txt 中应该给出我想要显示的字符串:
function output_txt = myfunction(~,event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text (string or cell array
% of strings)
在event_obj中有Position和Target,其中Position是一个数组,指定了光标的x,y,(和z)坐标,而Target是图形对象的句柄包含数据点。
更多信息位于:http://it.mathworks.com/help/matlab/ref/datacursormode.html
我想到了一个可能的解决方案:当我在包含数据点(目标)的图形对象的句柄中绘制它时,记住带有每个图名称的字符串,但我不知道它是否是可能,如果是,如何做到这一点。
还有其他解决办法吗?
由于您在调用 plot
时知道名称,因此可以将名称存储在绘图对象的 DisplayName
或 UserData
字段中。然后,您可以从 UpdateFcn
回调中访问这些。
以DisplayName
为例:
plot(rand(10, 1), 'DisplayName', 'a');
plot(rand(10, 1), 'DisplayName', 'b');
plot(rand(10, 1), 'DisplayName', 'c');
function updateFcn(~, event_obj)
name = get(event_obj.Target, 'DisplayName');
% Do something with name here
end