从 Ginput 获取子图编号

Get subplot number from Ginput

考虑以下示例

f = figure(1);
ax(1)  = subplot(2,1,1);
plot(1:100,randi(50,1,100));
ax(2)  = subplot(2,1,2);
plot(1:100,randi(50,1,100))
[x,  ~] = ginput(2);  
clickedAx = gca

无论如何,我是否可以从坐标轴句柄属性中获取我在其上单击以获取 ginput 的子图编号?或者其他方式?

您可以使用 gca 为您提供被点击的坐标轴,并使用 ismember 将其与您的 axes 数组进行比较。

[~, axnum] = ismember(gca, ax);

如果您不喜欢指定两个输出参数,您也可以这样写

axnum = find(ismember(ax, gca));