簡體   English   中英

從圖中的軸獲取當前選定的數據點

[英]Get currently selected data point from axes in a figure

我有一個 MATLAB 圖,其軸包含散點 plot。 這個散點圖 plot 上的每個點都有一個與之關聯的信號數據數組。 我想將用戶輸入作為點選擇,從分散 plot 和 plot 中的其他軸上的相應信號數據到同一圖。

將第二個軸的全局定義與數據游標上的 UpdateFcn 結合起來。 請參見下面的示例,該示例基於所選的隨機變量生成正弦波。

function getSelectedDataPoint()
% create figure
fig = figure;
% make second axes a global to adress in myupdatefcn
global ax2
% define axes
ax1 = axes('parent',fig,'position',[0.05 0.05 0.9 0.4]);
ax2 = axes('parent',fig,'position',[0.05 0.55 0.9 0.4]);
% Random scatter
scatter(ax1,rand(25,1),rand(25,1),25,'filled')
% Set datacursormode to on
dcm_obj = datacursormode(fig);
datacursormode on
% Specify objective function for clock
set(dcm_obj,'UpdateFcn',@myupdatefcn)

% Define objective function
function text = myupdatefcn(~,obj)
text = sprintf('X: %f \n Y: %f',[obj.Position(1),obj.Position(2)]);
% Find corresponding signal
id = find(and(obj.Position(1) == obj.Target.XData,obj.Position(2) ==             
obj.Target.YData));
% Do your thing with the signals
x = 0:0.1:100;
y = sin(obj.Target.XData(id)*x);
% plot on second axes
plot(ax2,x,y)
end
end

我不確定我是否完全理解您想要做什么,但您可能希望使用 MATLAB 中許多對象可用的“標簽”選項。

將“隨機散布”下的行替換為:

% Random scatter
hold(ax1,'on')
scatTag = cell(1,10);
for i = 1:10
scatTag{i} = scatter(ax1,rand(1,1),rand(1,1),25,'filled');
scatTag{i}.Tag = num2str(i);
end

在數據cursor更新function中,替換行“id=find(...”

tagname = obj.Target.Tag;

並修改信號 function 以指向您的目標標簽,無論 function 是什么。 在我的示例中,您可以這樣做來定義 y 值:

y = sin(str2double(tagname)*x);

它將根據標簽生成另一個正弦波。

希望這可以幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM