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

13 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Jun. de 2020
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 comentarios
Walter Roberson
Walter Roberson el 26 de Jun. de 2020
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 el 26 de Jun. de 2020
Thank you so much! It works perfectly!

Iniciar sesión para comentar.

Más respuestas (1)

Takumi
Takumi el 26 de Jun. de 2020
Editada: Takumi el 26 de Jun. de 2020
"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 comentario
matlabber
matlabber el 26 de Jun. de 2020
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?

Iniciar sesión para comentar.

Categorías

Más información sobre Frequency Transformations en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by