Adding clickable hyperlink to data tip label or plot point

9 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2023-8-29
"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 个评论
Zak
Zak 2023-8-29
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})

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by