Just like displaying coordinates, I want to display row and column number

7 ビュー (過去 30 日間)
matlabber
matlabber 2020 年 6 月 26 日
コメント済み: matlabber 2020 年 6 月 26 日
Hi, so I am gonna plot a bunch of different graphs from a 3D matrix and I want to be able to click on the line in the graph and be able to know from which row and column the line comes from. So I tried the following tactic from enable data cursor mode:
function txt = displayCoordinates(~,info)
x = info.Position(1);
y = info.Position(2);
txt = ['(' num2str(x) ', ' num2str(y) ')'];
end
x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
for j=1:4
plot(x,squeeze(y(i,j,:)))
hold on
dcm = datacursormode;
dcm.Enable = 'on';
%dcm.DisplayStyle = 'window';
dcm.UpdateFcn = @displayCoordinates;
end
end
So the clicking on the line works perfectly fine. The only problem is that I can't seem to implement how to display the row and column instead of the coordinates. So I tried this, but it doesn't work.
function txt = displayCoordinates(~,info)
txt = ['row is ' num2str(i) ', column is' num2str(j)];
end
Does anyone know how I can make this work?

採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 26 日
x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
for j=1:4
plot(x,squeeze(y(i,j,:)), 'UserData', [i j])
hold on
end
end
dcm = datacursormode;
dcm.Enable = 'on';
%dcm.DisplayStyle = 'window';
dcm.UpdateFcn = @displayCoordinates;
function txt = displayCoordinates(hObject,info)
ud = hObject.UserData;
if numel(ud) ~= 2
x = info.Position(1);
y = info.Position(2);
txt = sprintf('(x,y) = (%g, %g)', x, y);
else
txt = sprintf('row is %d, column is %d', ud);
end
end
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 26 日
x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
for j=1:4
plot(x,squeeze(y(i,j,:)), 'UserData', [i j])
hold on
end
end
dcm = datacursormode;
dcm.Enable = 'on';
%dcm.DisplayStyle = 'window';
dcm.UpdateFcn = @displayCoordinates;
function txt = displayCoordinates(~,info)
ud = info.Target.UserData;
if numel(ud) ~= 2
x = info.Position(1);
y = info.Position(2);
txt = sprintf('(x,y) = (%g, %g)', x, y);
else
txt = sprintf('row is %d, column is %d', ud);
end
end
matlabber
matlabber 2020 年 6 月 26 日
Thank you so much! It works perfectly!

サインインしてコメントする。

その他の回答 (1 件)

Takumi
Takumi 2020 年 6 月 26 日
編集済み: Takumi 2020 年 6 月 26 日
"info" has two fields and one of fields that named "Target" has line data.
So I atempted to use the find function to find an index that matches the data in the "Target" and the "Position".
x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
for j=1:4
plot(x,squeeze(y(i,j,:)))
hold on
dcm = datacursormode;
dcm.Enable = 'on';
%dcm.DisplayStyle = 'window';
dcm.UpdateFcn = @displayCoordinates;
end
end
function txt = displayCoordinates(~,info)
x = info.Position(1);
y = info.Position(2);
i = find(info.Target.XData==x);
j = find(info.Target.YData==y);
txt = ['row is ' num2str(i) ', column is' num2str(j)];
end
But this method is not robust...
  1 件のコメント
matlabber
matlabber 2020 年 6 月 26 日
This looks perfect, the only thing is that it doesn't seem to work properly.. It either shows me: "row is 1, column is 1" or "row is 2, column is 2", when clicking on different lines.
Would you know how to fix that?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by