首页 > 解决方案 > 通过单击点图中的某些点打开子图(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

标签: matlab

解决方案


推荐阅读