Identify lines in a plot by clicking on them
19 次查看(过去 30 天)
显示 更早的评论
hi,
I have to import a bunch of .txt files (approx. 200) from a folder and create a line plot of the first vs. third column.
my code is:
input_folder = uigetdir('start_path','dialog_title');
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {files.name});
for i = 1 : numel(file_paths)
data = readmatrix(file_paths{i},'Delimiter',{',','='});
plot(data(:,1),data(:,3),'-r');
hold on
end
Now I would like to be able to identify (i.e. get the corresponding file name) the lines in the plot by clicking on them. Any ideas on how to achieve such a result?
Thanks
3 个评论
Walter Roberson
2024-1-26
It would help if you were to
[~,partname] = fileparts(file_paths{i});
plot(data(:,1), data(:,3), '-r', 'displayname', partname);
采纳的回答
Pavan Sahith
2024-2-6
Hello Gino,
I've noticed that you're working with multiple .txt files to create line plots and you're interested in finding a way to identify which line corresponds to which file by simply clicking on it.
If you want to identify lines in the plot by clicking on them, you can try using the “UserData” property of the plot object and a custom “data cursor” update function.
I've put together a code example that will generate a plot with lines for each of your text files. When you interact with a line on this plot, a data cursor will pop up showing the X and Y coordinates of the selected point, as well as the name of the related file.
This method uses an update function for the "data cursor mode" that fetches the file's name from the "UserData" property attached to each plotted line.
Please refer to the following MathWorks documentation to know more about
Datacursormode- https://in.mathworks.com/help/matlab/ref/matlab.graphics.shape.internal.datacursormanager.html
Hope this helps
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!