Effacer les filtres
Effacer les filtres

Adding clickable hyperlink to data tip label or plot point

21 vues (au cours des 30 derniers jours)
Zak
Zak le 29 Août 2023
Modifié(e) : Zak le 29 Août 2023
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

Réponse acceptée

Voss
Voss le 29 Août 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 commentaires
Zak
Zak le 29 Août 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})
Voss
Voss le 29 Août 2023
Perfect!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by