필터 지우기
필터 지우기

Opening a subplot by clicking onto certail point in the point graph

조회 수: 2 (최근 30일)
MS
MS 2021년 2월 21일
답변: Divyanshu 2024년 4월 5일
Hello to everyone, I would like to ask for help regarding the action, which would allow to create a subplot by clicking onto certain point within the point graph. As a solution, I´ve found a relatively similar topic here:
However, it didn´t solved by problem since do not how to include the data into the function, which will be furher used for the plotting action. I am attaching the minium working example.
Thank you for yuur help.
% 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

답변 (1개)

Divyanshu
Divyanshu 2024년 4월 5일
Hi MS,
The Code provided in the description accurately detects the point clicked in the first plot. Moreover the function 'showZValueFcn' is triggered when a point is clicked in the first plot.
Here is the modified code which now creates a sub-plot when a point is clicked in the first plot:
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));
data=[15 18;34.5 45.1];
f = figure('Renderer', 'painters', 'Position', [50 50 1000 600]);
axh = axes(f);
x=data(:,1);
y=data(:,2);
tiledlayout(1,2);
nexttile;
h = plot(x,y,'bo','MarkerSize',4,'LineWidth',1)
xlabel('Cas [sec]');
ylabel('Amplituda [dBAE]')
h.ButtonDownFcn = @showZValueFcn;
function [coordinateSelected, minIdx] = showZValueFcn(hObj,event)
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,:);
nexttile;
plot([1:5],[1:5]);
end
However, the sub-plot just plots the dummy data and should be modified as per the use-case.
You can refer the following documentation for further details about 'tiledlayout':
Hope it helps!

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by