How to use and edit rows and values of a data tip when created by mouseclick?
20 views (last 30 days)
Show older comments
(see photo first)
I want to add a third data tip row, (Index, Value, and new option: filename) for datatip points that shows the filename of the point selected. I already have an array that can be indexed to access the filename at a certain index.
I am trying to make the datatip points use the index value of the data tip point for logic to assign the third row to be the filename of where the data is coming from.
How do I get the values (x,y) of the data tip that I just created by clicking the mouse?
Can I use the values of the data tip that I just created to assign a 3rd value to the already existing data tip?
Thanks.
0 Comments
Accepted Answer
More Answers (1)
Hassaan
on 20 Feb 2024
Edited: Hassaan
on 20 Feb 2024
% Adjust as per your requirment
% Example data
x = 1:10; % x values assumed
y = rand(1, 10); % y values assumed
filenames = arrayfun(@(n) sprintf('file_%d.txt', n), 1:10, 'UniformOutput', false); % filenames assumed
% Plot the data
fig = figure;
plt = plot(x, y, 'o'); % Using circular markers
% Create a new data tip row for filenames using the custom function
row = dataTipTextRow('Filename', @(xVal, yVal) filenameFromX(xVal, x, filenames));
% Attach the new row to the DataTipTemplate of the plot
plt.DataTipTemplate.DataTipRows(end+1) = row;
% Create a custom function that maps the x-value of a data point to a filename
function name = filenameFromX(xVal, xData, fileNames)
% Find the index of the closest x-value in the xData array
[~, idx] = min(abs(xData - xVal));
% Use the index to get the corresponding filename
name = fileNames{idx};
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!