Getting the value on plot curve with click and evaluate with the value (プロットしたグラフ​上の値をクリックして​得て、その値を計算に​使いたい)

조회 수: 1 (최근 30일)
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.

채택된 답변

Hiroshi Iwamura
Hiroshi Iwamura 2023년 9월 25일
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
高木 範明
高木 範明 2023년 10월 20일
今回も詳細にご教示いただき、ありがとうございました。大変よくわかりました。特に、propertiesでコールバックさせる場合の引数が(app, src, event)であることをおしえていただき、助かりました。

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Biotech and Pharmaceutical에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!