Options for displaying data by clicking on a marker in a plot

Hello,
is there an option for displaying data in addition to the usual x and y data by clicking on a marker in a plot?
I've plottet the coordinates of broken particles in the xy-plane and I want to display their ID in addition to their respective coordinates. The ID should be disyplayed in the same window as the x and y value (see attachement).

 Accepted Answer

For R2018b and later:
X = rand(10,1);
Y = rand(10,1);
IDs = randi(100,10,1);
s = scatter(X, Y, 'x');
s.DataTipTemplate.DataTipRows(3) = dataTipTextRow('ID', IDs);
For R2018a and earlier, something similar to this:
X = rand(10,1);
Y = rand(10,1);
IDs = randi(100,10,1);
f = figure;
scatter(X, Y, 'x');
dcm = datacursormode(f);
dcm.UpdateFcn = {@customDataTip, IDs};
function txt = customDataTip(~, eventData, IDs)
s = eventData.Target;
pos = eventData.Position;
ID = IDs(s.XData == pos(1) & s.YData == pos(2));
valueFormat = ' \color[rgb]{0 0.6 1}\bf';
removeValueFormat = '\color[rgb]{.25 .25 .25}\rm';
txt = {['X',[valueFormat num2str(pos(1)) removeValueFormat]],...
['Y',[valueFormat num2str(pos(2)) removeValueFormat]],...
['ID',[valueFormat num2str(ID) removeValueFormat]]};
end

6 Comments

Hi Tommy,
thank you for your solution.
Is it possible to display the entries of the ID column (see attached brokenPart.txt file)? The given code displays the indizes of the ID-column as IDs.
Hmm, I might've messed up somewhere but I'm having a hard time figuring out where. Which answer did you use? And could you provide the code you're using to load and plot your data?
I've implemented your first answer. The used .txt files are attached and the complete code is the following:
clear all
clc
%Importing data from walls.txt:
A1 = importdata('walls.txt ');
A = A1.data(1:256,:);
%Importing data from brokenPart.txt
A2 = importdata('brokenPart.txt');
P = A2.data;
Ax = A(:,5);
Ay = A(:,6);
Az = A(:,7);
Bx = A(:,8);
By = A(:,9);
Bz = A(:,10);
Cx = A(:,11);
Cy = A(:,12);
Cz = A(:,13);
x = [Ax; Bx; Cx];
y = [Ay; By; Cy];
z = [Az; Bz; Cz];
Px = P(:,9);
Py = P(:,10);
Pz = P(:,11);
tsim = P(:,1);
figure(1)
plot([min(x),max(x)],[min(z),min(z)],'-k','LineWidth',2)
hold on
plot([max(x),max(x)],[min(z),max(z)],'-k','LineWidth',2);
plot([max(x),min(x)],[max(z),max(z)],'-k','LineWidth',2);
plot([min(x),min(x)],[max(z),min(z)],'-k','LineWidth',2);
s = scatter(Px,Pz,50,tsim,'x');
s.DataTipTemplate.DataTipRows(3) = dataTipTextRow('ID', A(:,2));
hold off
xlabel('x [m]');
ylabel('z [m]');
c = colorbar;
c.Label.String = 'tsim [s]';
figure(2)
plot(Ax,Ay,'-k','LineWidth',2)
hold on
s = scatter(Px,Py,50,tsim,'x');
s.DataTipTemplate.DataTipRows(3) = dataTipTextRow('ID', A(:,2));
hold off
xlabel('x [m]');
ylabel('y [m]');
c = colorbar;
c.Label.String = 'tsim [s]';
Assuming your IDs are contained in the second column of brokenParts.txt and not walls.txt, I believe you should pull them from P rather than A. For example:
s.DataTipTemplate.DataTipRows(3) = dataTipTextRow('ID', P(:,2));
You're currently grabbing the IDs from walls.txt - it just so happens that the second column of walls.txt contains row numbers, making the 'IDs' seem like indices.
Hi Tommy,
your solution works exactly as it should. Thank you very much for the support.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 8 Jun 2020

Commented:

on 18 Jun 2020

Community Treasure Hunt

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

Start Hunting!