matlab - 通过单击点图中的某些点打开子图(MATLAB)
问题描述
我想寻求有关该操作的帮助,这将允许通过单击点图中的某个点来创建子图。作为解决方案,我在这里找到了一个相对相似的主题: https ://uk.mathworks.com/matlabcentral/answers/475853-how-to-enable-a-figure-so-that-if-i-click- on-a-point-and-it-will-show-the-value 但是,它并没有解决问题,因为不知道如何将数据包含到函数中,这将进一步用于绘图操作。我附上了最小的工作示例。谢谢你的帮助。
% Let's have a cell array, which contains wave transients, where in each cell, there's a time vector and corresponding signal
%Cell array containing two signals (time axis + voltage)
HIT_DB{1}(:,1)=0:0.01:1;
HIT_DB{1}(:,2)=sin(2*pi*HIT_DB{1}(:,1));
HIT_DB{2}(:,1)=0:0.01:2;
HIT_DB{2}(:,2)=sin(4*pi*HIT_DB{2}(:,1));
%To this cell array belongs an array of two points, where point #1 [15,18] correspons to signal stored in HIT_DB{1},
% while point #2 [34.5 45.1] corresponds to signal stored in HIT_DB{2}
data=[15 18;34.5 45.1];
%According to the link above, it is possible to get the index of point onto which has been clicked,
% but now I cannot figure out how to perform the plotting of the corresponding signal.
figure('Renderer', 'painters', 'Position', [50 50 1000 600])
axh = axes();
x=data(:,1);
y=data(:,2);
h=plot(axh,x,y,'bo','MarkerSize',4,'LineWidth',1)
xlabel('Cas [sec]');
ylabel('Amplituda [dBAE]')
h.ButtonDownFcn = @showZValueFcn;
function [coordinateSelected, minIdx] = showZValueFcn(hObj,event)
% FIND NEAREST (X,Y,Z) COORDINATE TO MOUSE CLICK
% Inputs:
% hObj (unused) the axes
% event: info about mouse click
% OUTPUT
% coordinateSelected: the (x,y,z) coordinate you selected
% minIDx: The index of your inputs that match coordinateSelected.
x = hObj.XData;
y = hObj.YData;
z = zeros(length(x),1);
pt = event.IntersectionPoint % The (x0,y0,z0) coordinate you just selected
coordinates = [x(:),y(:),z(:)]; % matrix of your input coordinates
dist = pdist2(pt,coordinates); %distance between your selection and all points
[~, minIdx] = min(dist); % index of minimum distance to points
coordinateSelected = coordinates(minIdx,:); %the selected coordinate
% from here you can do anything you want with the output. This demo
% just displays it in the command window.
fprintf('[x,y,z] = [%.5f, %.5f, %.5f]\n', coordinateSelected)
%disp(num2str(Hit_params(minIdx,1)))
end % <--- optional if this is embedded into a function
解决方案
推荐阅读
- sql-server - 当我尝试将字符串列转换为浮点列时出错
- c# - 如何对数据库更改执行集线器方法
- react-native - 我们可以使用 react-native 从 onesignal 获取应用程序内的未读通知吗?
- c - 当剩余内存超过一定大小时,我的自定义 malloc 函数崩溃
- xml - 如何在 FSharp.Data.XmlProvider 中使用保留字?
- python - 如何在 python 2.7 中获取随机整数
- java - LocalDateTime Java - 获取下个月的同一天
- jwt - JWT DefaultAuthenticateScheme 是如何工作的?
- spring - 为什么 App Engine 无法使用我的 Spring Boot 应用连接到 Cloud SQL?
- kubernetes - 未使用 Keycloack/Dex 授权的 Kubernetes 仪表板