Clear Filters
Clear Filters

Adding clickable hyperlink to data tip label or plot point

19 views (last 30 days)
I want to add a row to a plot data tip that is a hyperlink. Eventually I want it to go to a file on my computer, but for now I need to just figure out how to get the hyperlink into the data tip. I've created a simple script where I have a table with a sample number, three values, and a link. I can make a plot where I add the sample number as a row to the data tip information, but how can I add another row with a clickable hyperlink?
It doesn't necessarily need to be done through the data tip, but somehow I'd like to be able to click on a point and have it lead to a corresponding link.
% Sample data
sampleData = [
1, 0.1, 0.2, 0.3;
2, 0.5, 0.6, 0.7;
3, 0.8, 0.9, 1.0
];
links = repmat({'https://www.mathworks.com'},3,1);
% Create the table
sampleTable = table(...
sampleData(:, 1), ... % "sample" column
sampleData(:, 2), ... % "x" column
sampleData(:, 3), ... % "y" column
sampleData(:, 4), ... % "z" column
links);
figure
hold on
for i = 1:height(sampleTable) % using a loop since eventually I will color the points depending on the value of the "z" column
point = plot(sampleTable.Var2(i), sampleTable.Var3(i), '.','MarkerSize',20);
urow = dataTipTextRow("Sample",sampleTable.Var1(i));
point.DataTipTemplate.DataTipRows(end+1) = urow;
end
hold off

Accepted Answer

Voss
Voss on 29 Aug 2023
"somehow I'd like to be able to click on a point and have it lead to a corresponding link"
You can do that with the line's ButtonDownFcn.
% Sample data
sampleData = [
1, 0.1, 0.2, 0.3;
2, 0.5, 0.6, 0.7;
3, 0.8, 0.9, 1.0
];
links = repmat({'https://www.mathworks.com'},3,1);
% Create the table
sampleTable = table(...
sampleData(:, 1), ... % "sample" column
sampleData(:, 2), ... % "x" column
sampleData(:, 3), ... % "y" column
sampleData(:, 4), ... % "z" column
links);
figure
hold on
for i = 1:height(sampleTable) % using a loop since eventually I will color the points depending on the value of the "z" column
point = plot(sampleTable.Var2(i), sampleTable.Var3(i), '.', ...
'MarkerSize',20,'ButtonDownFcn',@(~,~)web(sampleTable.links{i}));
end
hold off
  2 Comments
Zak
Zak on 29 Aug 2023
Awesome!! Thanks so much. I replaced the web() function with winopen() to open a file and it does exactly what I was looking for if I put file paths in the links field!
'ButtonDownFcn',@(~,~)winopen(sampleTable.links{i})

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!