フィルターのクリア

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 Center および File ExchangeAnnotations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by