Getting the value on plot curve with click and evaluate with the value (プロットしたグラフ上の値をクリックして得て、その値を計算に使いたい)
1 view (last 30 days)
Show older comments
I would like to perform the following tasks. I would be happy to learn how to do that.
---- Create a graph with mlx file. (Plot)
Ia_start = 1
Ia_incre = 1
Ia_end = 3
figure
ax = gca;.
set(gcf, 'visible', 'on','Position',[400 50 1000 940])
hold on
for Ia = Ia_start : Ia_incre : Ia_end
fimplicit(@(id,iq) id.^2+iq.^2 - Ia.^2);
end
hold off
---- Click on a point on the graph plotted here to obtain its value.(for example)
x=0.672258
y=1.8836
---- For example, we want to compute and get the answer = x + y.
0 Comments
Accepted Answer
Hiroshi Iwamura
on 25 Sep 2023
figureのコールバックを設定することにより、カーソル位置が取得可能です。
また、必要であれば datatip を利用することで、関数上のデータを取得することもできます。
By setting up a callback for the figure, it becomes possible to obtain the cursor's position.
Additionally, if needed, you can also retrieve data from the function using a "datatip".
function test
Ia_start = 1;
Ia_incre = 1;
Ia_end = 3;
figure
ax = gca;
set(gcf, 'visible', 'on','Position',[400 50 1000 940])
hold on
for Ia = Ia_start : Ia_incre : Ia_end
fp(Ia) = fimplicit(@(id,iq) id.^2+iq.^2 - Ia.^2);
% append new data tip if necessary
row = dataTipTextRow("X + Y", @(x,y)(x+y));
fp(Ia).DataTipTemplate.DataTipRows(end+1) = row;
end
hold off
f = gcf;
ax.Units = "normalized";
axWidth = f.Position(3) * ax.Position(3);
axHeight = f.Position(4) * ax.Position(4);
axSX = f.Position(3) * ax.Position(1);
axSY = f.Position(4) * ax.Position(2);
axXmin = ax.XLim(1);
axXmax = ax.XLim(2);
axYmin = ax.YLim(1);
axYmax = ax.YLim(2);
f.WindowButtonDownFcn = @getaxPos;
% mouse callback
function pos = getaxPos(src,~)
last_seltype = src.SelectionType;
C = get (gcf, 'CurrentPoint');
x = (C(1) - axSX) / axWidth * (axXmax - axXmin) + axXmin;
y = (C(2) - axSY) / axHeight * (axYmax - axYmin) + axYmin;
pos = [x, y];
if strcmp(last_seltype,'normal') % left click
% find data on the fimplicit function using "datatip"
for Ia = Ia_start : Ia_incre : Ia_end
dt(Ia) = datatip(fp(Ia),x,y);
dist(Ia) = norm([x,y] - [dt(Ia).X, dt(Ia).Y]);
end
[~,idx] = min(dist); % find nearest data from 3 fimplicits
for Ia = Ia_start : Ia_incre : Ia_end
if Ia==idx
dt(Ia).Visible = 'on';
z = dt(Ia).X + dt(Ia).Y % disp z to command window
else
dt(Ia).Visible = 'off'; % turn off other datatips
end
end
end
end
% set (f, 'WindowButtonDownFcn', '');
end
7 Comments
More Answers (0)
See Also
Categories
Find more on Biotech and Pharmaceutical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!