App Designerで複​数の書式の異なるグラ​フでカーソル測定する​にはどうすればいいで​すか

4 次查看(过去 30 天)
健志
健志 2022-7-2
以下3つのグラフをPanelに定義し、それぞれのグラフでマウスクリックした座標に最も近いデータのカーソル測定するにはどのように修正すればいいでしょうか?
1)XY軸ともにLinear
2)X軸Log、Y軸Linear
3)X軸Linear、Y軸Log
properties (Access = private)
subtitle % Description
ax1
ax2
ax3
line_h1
line_h2
line_h3
% Description
hP = gobjects(3,1); % ButtonDownFcn で描画する点のハンドル(初期設定)
hT = gobjects(3,1); % ButtonDownFcn で描画するテキストのハンドル(初期設定)
end
methods (Access = private)
function ButtonDownFcn(app,src,evnt)
% Axes の ButtonDownFcn コールバック
Cp = get(src,'CurrentPoint'); % 座標軸上のマウスの位置を取得
H = findobj([app.line_h1,app.line_h2, app.line_h3],'Parent',src); % 子オブジェクトとなるLine のハンドル取得
% 最近傍のデータ点のインデックス
[~,ind] = min(sqrt((Cp(1,1)-H.XData).^2+(Cp(1,2)-H.YData).^2));
% hold(src,"on")
ax_n = src.UserData; %座標の場所を示す番号
if isgraphics(app.hP(ax_n)) %有効なグラフィックか検証
delete(app.hP(ax_n))
delete(app.hT(ax_n))
drawnow
end
if ax_n == 1
app.hP(ax_n) = plot(src,H.XData(ind),H.YData(ind),'rx');
app.hT(ax_n) = text(src,H.XData(ind),H.YData(ind)+0.2,num2str(H.YData(ind)));
elseif ax_n == 2
app.hP(ax_n) = semilogx(src,H.XData(ind),H.YData(ind),'rx');
app.hT(ax_n) = text(src,H.XData(ind),H.YData(ind)+0.2,num2str(H.YData(ind)));
else
app.hP(ax_n) = semilogy(src,H.XData(ind),H.YData(ind),'rx');
app.hT(ax_n) = text(src,H.XData(ind),H.YData(ind)+0.2,num2str(H.YData(ind)));
end
% hold(src,"off")
end
function [line_h] = selectplot(app,ax, x,y,ch)
if ch == 1
line_h = plot(ax, x,y,'HitTest',"off");
elseif ch == 2
line_h = semilogx(ax, x,y,'HitTest',"off");
else
line_h = semilogy(ax, x,y,'HitTest',"off");
end
end
end
x = 1:10;
y = rand(1,10);
app.subtitle = tiledlayout(app.Panel,3,1);
app.ax1 = nexttile(app.subtitle);
app.ax1.NextPlot = 'add'; % 重ね書き(Axes のプロパティを引き継いだままプロット重ね書き)
app.ax1.ButtonDownFcn = @app.ButtonDownFcn; % コールバック設定
app.ax1.UserData = 1; %判別用の値
% app.line_h1 = plot(app.ax1, x,y,'HitTest',"off");
app.line_h1 = selectplot(app, app.ax1, x,y,1);
app.ax2 = nexttile(app.subtitle);
app.ax2.NextPlot = 'add';
app.ax2.ButtonDownFcn = @app.ButtonDownFcn;
app.ax2.UserData = 2; %判別用の値
% app.line_h2 = plot(app.ax2, x,y,'HitTest',"off");
app.line_h2 = selectplot(app, app.ax2, x,y*2,2);
app.ax3 = nexttile(app.subtitle);
app.ax3.NextPlot = 'add';
app.ax3.ButtonDownFcn = @app.ButtonDownFcn;
app.ax3.UserData = 3; %判別用の値
% app.line_h3 = plot(app.ax3, x,y,'HitTest',"off");
app.line_h3 = selectplot(app, app.ax3, x,y*3,3);

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 アプリの作成 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!